 |
 |
| View previous topic :: View next topic |
| Author |
Message |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Tue Jan 11, 2011 12:07 pm |
|
|
| Quote: |
if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE)) {
top_value = MAX_VALUE; // Stop the Servo Motor
} else {
if (ldr_diff > THRESHOLD_VALUE) {
top_value = CCW_ROTATION; // Counterclockwise Rotation
} else {
top_value = CW_ROTATION; // Clockwise Rotation
}
} |
For the negative, is math.h header enough or do I need more than that? thanx |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Tue Jan 11, 2011 3:31 pm |
|
|
You don't need math.h. Simple +/-* are part of the standard compiler.
However you _do_ need to be using a number type for the variable, that supports being 'negative' (signed).
Best Wishes |
|
 |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Tue Jan 11, 2011 9:46 pm |
|
|
If I write this
can the compiler know that it is a negative value of threshold?
Thank you |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Wed Jan 12, 2011 3:23 am |
|
|
Yes, provided 'THRESHOLD_VALUE' is declared as _signed_.
Best Wishes |
|
 |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Wed Jan 12, 2011 5:44 am |
|
|
thanx for replying
| Code: | void main()
{
unsigned char ldr_left ;
unsigned char ldr_right ;
int ldr_diff ;
port_b_pullups(TRUE);
setup_adc_ports(ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_8 );
setup_spi(SPI_SS_DISABLED);
setup_comparator(NC_NC_NC_NC);
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
lcd_init(); // Always call this first.
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab
set_tris_A(0xFF); //port a set as input
set_tris_B(0x00); //port b set as output
output_B(0x00);
// Init Servo Pulse
pulse_max=0;
pulse_top=0;
top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop
// Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
set_timer0(206) ; //interrupt every 0.1ms
enable_interrupts(int_timer0);
enable_interrupts(GLOBAL);
// Read the ADC here
for(;;){
set_adc_channel(0);
while ( input(PIN_A0) ) {
delay_ms( 5000 );
ldr_left = read_adc();
lcd_putc("\fHello World\n");
}
read_adc(ADC_START_ONLY);
ldr_left=read_adc(ADC_READ_ONLY);
|
i wan to read my adc and sent to a lcd
but when i try it in the proteus and my hardware
protues will show that mcu is busy
how to solve that.
thank you |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9658 Location: Greensville,Ontario
|
|
Posted: Wed Jan 12, 2011 6:31 am |
|
|
You should actually read the CCS C manual a few more times, and look at all the examples they've supplied.
The easiest error to see is that you have enabled interrupts without providing an ISR.
This is totally wrong !!!
There are a few more, depending on the PIC you're using...
Calls to functions without code
,using the TRISx(..) statement is for experts NOT beginners(let the compiler do the work, tht's WHY you bought it!)
no headers...
no includes...
no reference to functions
As for Proteus, I do not use it as it's full of bugs,errors,etc. frankly not worth the disk space it takes up! |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Wed Jan 12, 2011 8:14 am |
|
|
The emulation is probably just sitting in the delay_ms(5000).
The emulation runs well below 'real' speed. This will take several _minutes_ to complete...
If pin A0, is low, the code will drop of the end as shown, and go to sleep.
Are you reading an 8bit value from the ADC, or a ten bit value?.
If there is no interrupt handler, the code _will_ hang.
Best Wishes |
|
 |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Wed Jan 12, 2011 9:05 am |
|
|
for pic16f877a ,it can read up to 10bit adc
should i read 8 bit or 10bit
as for 10 bit ,the lsb 2 bit should be ignore isnt it? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Wed Jan 12, 2011 10:04 am |
|
|
If the code has a #device ADC=10, then the ADC functions will return a ten bit value. ADC=8, and the function will return an 8bit value.
Which you need, depends on what accuracy you want, but if you are using/want the full accuracy of the ADC, you need to be using values larger than an int8. There is also a potential problem. If you read into an int8, and then want to develop a -ve version of this value, this _won't fit_ in an int8 variable. The sign bit uses a bit of the value, leaving just seven bits for the numeric range. You need a signed int16 value, if you want -ve handling with an 8bit number....
Best Wishes |
|
 |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Wed Jan 12, 2011 1:48 pm |
|
|
Thanx Ttelmah
| Code: |
#include <16F877A.h>
#device adc=10
#use delay(clock=4000000)
#define XTAL_FREQ 40000000
#include <limits.h>
#include <flex_lcd.c>
#FUSES XT, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#use fast_io(B)
#use fast_io(A)
#byte ADRESL = 0xF9E
#byte ADRESH = 0xF1E
#define MAX_VALUE 200
#define CCW_ROTATION MAX_VALUE - 25
#define CW_ROTATION MAX_VALUE - 15
#define STOP_ROTATION MAX_VALUE
#define THRESHOLD_VALUE 30
#define A0 PIN_A0
#define A1 PIN_A1
#define B1 PIN_B1
#BIT ADFM = 0x9F.7 //used to choose ADC result justification
int8 pulse_max=0;
int8 pulse_top=0;
int8 top_value=0 ;
#INT_TIMER0
static void TIMER0_isr(void)
{
if(INT_TIMER0)// TIMER0 Interrupt Flag
{
pulse_max++; // Pulse Max Increment
pulse_top++; // Pulse Top Increment
/* MAX_VALUE=200 turn off the pulse */
if (pulse_max >= MAX_VALUE) {
pulse_max=0;
pulse_top=0;
output_low(B1); // Turn off RB1
}
if (pulse_top == top_value) {
output_high(B1); // Turn On RB1
}
set_timer0(206) ; // Initial Value for 0.1ms Interrupt
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
}
}
void main(void)
{
int16 ldr_left=0 ;
int16 ldr_right=0 ;
int16 ldr_diff ;
port_b_pullups(TRUE);
setup_adc_ports(ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_8);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_A(0xFF); //port a set as input
set_tris_B(0x00); //port b set as output
output_B(0x00);
// Init Servo Pulse
pulse_max=0;
pulse_top=0;
top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop
// Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
set_timer0(206) ; //interrupt every 0.1ms
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
// Read the ADC here
for(;;){
A0;
ADFM=0; //1 for ADC result to be right-justified, 0 for left-justified
set_adc_channel(0);
delay_us(10);
ldr_left=read_adc();
read_adc(ADC_START_ONLY);
ldr_left=read_adc(ADC_READ_ONLY);
[color=red] printf(lcd_putc, "value = %lu \n",ldr_left);[/color]
A1;
set_adc_channel(1);
delay_us(10);
ldr_right=read_adc();
read_adc(ADC_START_ONLY);
ldr_right=read_adc(ADC_READ_ONLY);
// Get the different
ldr_diff = ldr_left - ldr_right;
if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE))
{
output_low(B1); // Stop the Servo Motor
}
else
{
if (ldr_diff > THRESHOLD_VALUE)
{
top_value = CCW_ROTATION; // Counterclockwise Rotation
}
else
{
top_value = CW_ROTATION; // Clockwise Rotation
}
}
}
}
|
For the lcd code, if I delete all those inside void main(), I can display sentence in the lcd but I can't display adc value in the lcd. Is there anything I need to add? |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Wed Jan 12, 2011 3:57 pm |
|
|
| Code: |
ldr_right=read_adc();
//read_adc(ADC_START_ONLY);
//ldr_right=read_adc(ADC_READ_ONLY);
|
You only need to read the ADC once. You are reading it twice. The first line does _everything_ that is needed to read the ADC.
Same applies in your other reading.
| Code: |
signed int16 ldr_left=0 ;
signed int16 ldr_right=0 ;
signed int16 ldr_diff ;
|
If you want the difference to be able to go -ve, the values need to be _signed_...
| Code: |
// ADFM=0; //1 for ADC result to be right-justified, 0 for left-justified
|
Get rid of this. The PIC correctly returns the ADC right justified if you have selected ADC=10. You must not left justify the ADC value, if you want to be able to use signed arithmetic, since only 15bits are then available, you _must not_ left justify the ADC value....
What is in the ADC reading, should print, _unless_ your code is spending too much time in your timer interrupt. You are calling this potentially at a frequency that may well make all other code stop operating. It looks to be being clocked off CPU clock/2, with you presetting the counter to '206', taking then just 100 CPU instructions to be called (256-206)*2. The interrupt handler has a typical 'overhead' of about 60 instructions. If you have 40 instructions or more of code in your handler routine, this could prevent everything else from running.....
Best Wishes |
|
 |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Wed Jan 12, 2011 10:34 pm |
|
|
I'm using this formula to count:
| Code: |
Period = (256 - TMR0)*(4/fosc)*(Prescaler)
|
I want my period to be 0.1ms and thus prescaler and tmr0 can be a variable that you can put in. So for the prescaler I put in 2 and for the tmr I use 206. Should I use a bigger prescaler instead? |
|
 |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Thu Jan 13, 2011 2:20 am |
|
|
For prescaler 4 period 0.1ms
the TMR0=231
(256-231)*4=100 instruction
For prescaler 8 period 0.1ms
the TMR0=244
(256-244)*8=96 instruction |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20159
|
|
Posted: Thu Jan 13, 2011 3:26 am |
|
|
Seriously, increase the clock rate of the CPU.
0.1mSec, is just 100 instruction times for a PIC running at 4MHz. As already mentioned, getting into, and out of an interrupt, take significant time. You are 'pushing' the upper limit of what a 4MHz chip can service, and if you are doing anything at all significant in the interrupt, then this is the cause of your problems. Switch to (say) using a 20MHz crystal instead, and then if you use the /2 prescaler, and _don't_ load any value into the timer (let it 'free run'), you will get 9765 interrupts per second, and have a chance of things working.
I suspect if you post your interrupt code, you just don't have time to do it at the current processor speed....
Best Wishes |
|
 |
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Thu Jan 13, 2011 4:38 am |
|
|
| Quote: | #include <16F877A.h>
#device adc=10
#use delay(clock=20000000)
#define XTAL_FREQ 200000000
#include <limits.h>
#include <flex_lcd.c>
#FUSES XT, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#use fast_io(B)
#use fast_io(A)
#byte ADRESL = 0xF9E
#byte ADRESH = 0xF1E
#define MAX_VALUE 200
#define CCW_ROTATION MAX_VALUE - 25
#define CW_ROTATION MAX_VALUE - 15
#define STOP_ROTATION MAX_VALUE
#define THRESHOLD_VALUE 30
#define A0 PIN_A0
#define A1 PIN_A1
#define B1 PIN_B1
int8 pulse_max=0;
int8 pulse_top=0;
int8 top_value=0 ;
#INT_TIMER0
static void TIMER0_isr(void)
{
if(INT_TIMER0)// TIMER0 Interrupt Flag
{
pulse_max++; // Pulse Max Increment
pulse_top++; // Pulse Top Increment
/* MAX_VALUE=200 turn off the pulse */
if (pulse_max >= MAX_VALUE) {
pulse_max=0;
pulse_top=0;
output_low(B1); // Turn off RB1
}
if (pulse_top == top_value) {
output_high(B1); // Turn On RB1
}
set_timer0(0) ; // Initial Value for 0.1ms Interrupt
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
}
}
void main(void)
{
signed int16 ldr_left ;
signed int16 ldr_right ;
signed int16 ldr_diff ;
port_b_pullups(TRUE);
setup_adc_ports(ALL_ANALOG );
setup_adc(ADC_CLOCK_DIV_8);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
lcd_init();
set_tris_A(0xFF); //port a set as input
set_tris_B(0x00); //port b set as output
output_B(0x00);
// Init Servo Pulse
pulse_max=0;
pulse_top=0;
top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop
// Init TIMER0: Period: Fosc/4 x Prescaler x TMR0
set_timer0(0) ; //interrupt every 0.1ms
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
// Read the ADC here
for(;;){
A0;
set_adc_channel(0);
delay_us(10);
ldr_left=read_adc();
printf(lcd_putc, "value = %lu \n",ldr_left);
A1;
set_adc_channel(1);
delay_us(10);
ldr_right=read_adc();
printf(lcd_putc, "value = %lu \n",ldr_right);
// Get the different
ldr_diff = ldr_left - ldr_right;
if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE))
{
output_low(B1); // Stop the Servo Motor
}
else
{
if (ldr_diff > THRESHOLD_VALUE)
{
top_value = CCW_ROTATION; // Counterclockwise Rotation
}
else
{
top_value = CW_ROTATION; // Clockwise Rotation
}
}
}
}
|
there are something wrong with my printf line,as the code cant be compile
for the set timer0 part,is it correct that i just put 0. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|