 |
 |
View previous topic :: View next topic |
Author |
Message |
khongtrung
Joined: 24 Apr 2015 Posts: 4 Location: Viet Nam
|
timer0 and ADC not working pic16f877a!!!! |
Posted: Fri Apr 24, 2015 8:56 pm |
|
|
Hi every one!
I use 16f877a to read ADC and control RC servo motor. I have problem in interrupt timer0. INT_timer0 not working, but ADC work fine. i don't know why!!!
Thanks and regards!!!
Last edited by khongtrung on Sun Apr 26, 2015 6:36 am; edited 2 times in total |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9459 Location: Greensville,Ontario
|
|
Posted: Sat Apr 25, 2015 4:59 am |
|
|
Go back to 'basics'
Cut code ONLY for the LCD module,compile,download and test.
You might want to get rid of the use fastIO and use the CCS default of standard to let the compiler handle the DDRs.
You need to simplify your program( actually create an 'LCD only' one) then show us that code and what happens.
One hint: Put a delay_ms(500); BEFORE the LCD_init() function. every LCD module needs some time before the PIC can access it.How long is somewhere in the LCD datasheet but for the past 20 years 1/2 second has been more than enough for all the LCDs I've used.
Jay |
|
 |
khongtrung
Joined: 24 Apr 2015 Posts: 4 Location: Viet Nam
|
|
Posted: Sat Apr 25, 2015 6:49 am |
|
|
temtronic wrote: | Go back to 'basics'
Cut code ONLY for the LCD module,compile,download and test.
You might want to get rid of the use fastIO and use the CCS default of standard to let the compiler handle the DDRs.
You need to simplify your program( actually create an 'LCD only' one) then show us that code and what happens.
One hint: Put a delay_ms(500); BEFORE the LCD_init() function. every LCD module needs some time before the PIC can access it.How long is somewhere in the LCD datasheet but for the past 20 years 1/2 second has been more than enough for all the LCDs I've used.
Jay |
Thanks for your help!!! But my LCD work fine, my problem is timer0 does not work, does not interupt!!! |
|
 |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Apr 25, 2015 7:05 am |
|
|
I studied your code for some time and have to say I'm sorry but I've no clue as to what it is doing or why it is doing all these things.
Your code is difficult to understand because we have no clue as to what kind of hardware is connected and how it is connected and there is hardly any comments in your code.
As Temtronic already said, to find a bug the normal way is to remove everything not related to your problem. Then, with this smaller program you try to make things even simpler by writing to the LCD instead of outputs. Even better than the LCD output: use an In Circuit Debugger to step through your code, or use the free MPLAB simulator to do the same thing.
A few general remarks on your coding style:
- Write your program in the English language: comments, variable and function names. Do this as a habit, even when it is just you who will read ever it. There will be a time when you show your code to others, like now on this forum.
- Be consequent!!! It will help to prevent simple stupid bugs. For example, when you init the ports do that in alphabetical order, not "b,d,a,c,e" as you have now.
- Write all constants in full capital letters, it makes your code easier to understand. Now you have "rb0" but also "RC0".
- Write comments about what your code is doing. Reading the code we can see 'how' things are done, but we don't know why.
- Use sensible variable names. W, dem & mau don't mean anything to me.
- Use sensible #defined names for the output ports. We have no clue as to what is connected to rb0, rc0, etc. For example, use: Code: | #define SERVO_DIRECTION RC0
#define FORWARD 0
#define BACKWARD 1
SERVO_DIRECTION = FORWARD;
is easier to understand than:
RC0 = 0; |
You are writing very often to the EEPROM. Why?
Are you aware that the EEPROM can only be written a limited number of times? 100.000 to 1 million times sounds like a lot but do the math for how soon you reach 100k and you will be surprised by how quick that is at your writing speed.
The following fragment is difficult to understand: Code: | unsigned char w[8] = {1,2,4,8,16,32,64,128};
...
if(adc[u]<nguong[u])
{
sensor |= w[7-u];
}
else sensor |= 0;
| It can be replaced by the more efficient and easier to read: Code: | if (adc[u] < nguong[u])
{
bit_set(sensor, 7-u);
} |
|
|
 |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Apr 25, 2015 7:23 am |
|
|
Proteus does not run it for you? -
Your program header area sure looks like Proteus ..........
Got a schematic ? |
|
 |
khongtrung
Joined: 24 Apr 2015 Posts: 4 Location: Viet Nam
|
|
Posted: Sat Apr 25, 2015 8:08 am |
|
|
ckielstra wrote: | I studied your code for some time and have to say I'm sorry but I've no clue as to what it is doing or why it is doing all these things.
Your code is difficult to understand because we have no clue as to what kind of hardware is connected and how it is connected and there is hardly any comments in your code.
As Temtronic already said, to find a bug the normal way is to remove everything not related to your problem. Then, with this smaller program you try to make things even simpler by writing to the LCD instead of outputs. Even better than the LCD output: use an In Circuit Debugger to step through your code, or use the free MPLAB simulator to do the same thing.
A few general remarks on your coding style:
- Write your program in the English language: comments, variable and function names. Do this as a habit, even when it is just you who will read ever it. There will be a time when you show your code to others, like now on this forum.
- Be consequent!!! It will help to prevent simple stupid bugs. For example, when you init the ports do that in alphabetical order, not "b,d,a,c,e" as you have now.
- Write all constants in full capital letters, it makes your code easier to understand. Now you have "rb0" but also "RC0".
- Write comments about what your code is doing. Reading the code we can see 'how' things are done, but we don't know why.
- Use sensible variable names. W, dem & mau don't mean anything to me.
- Use sensible #defined names for the output ports. We have no clue as to what is connected to rb0, rc0, etc. For example, use: Code: | #define SERVO_DIRECTION RC0
#define FORWARD 0
#define BACKWARD 1
SERVO_DIRECTION = FORWARD;
is easier to understand than:
RC0 = 0; |
You are writing very often to the EEPROM. Why?
Are you aware that the EEPROM can only be written a limited number of times? 100.000 to 1 million times sounds like a lot but do the math for how soon you reach 100k and you will be surprised by how quick that is at your writing speed.
The following fragment is difficult to understand: Code: | unsigned char w[8] = {1,2,4,8,16,32,64,128};
...
if(adc[u]<nguong[u])
{
sensor |= w[7-u];
}
else sensor |= 0;
| It can be replaced by the more efficient and easier to read: Code: | if (adc[u] < nguong[u])
{
bit_set(sensor, 7-u);
} |
|
Thanks for your comment!! MY project is a robot following line, the line consist of black line and white line. The code:
Code: | unsigned char w[8] = {1,2,4,8,16,32,64,128};
...
if(adc[u]<nguong[u])
{
sensor |= w[7-u];
}
else sensor |= 0;
|
i need read adc from IR led then save the value adc, and compare. It can realize black line or white line. But the big problem is timer0 does not interupt! @@ |
|
 |
khongtrung
Joined: 24 Apr 2015 Posts: 4 Location: Viet Nam
|
|
Posted: Sat Apr 25, 2015 8:09 am |
|
|
asmboy wrote: | Proteus does not run it for you? -
Your program header area sure looks like Proteus ..........
Got a schematic ? |
My project can not run on proteus! |
|
 |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat Apr 25, 2015 9:23 am |
|
|
"proteus"
have you built ANY hardware at all yet ? |
|
 |
|
|
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
|