View previous topic :: View next topic |
Author |
Message |
freedom
Joined: 10 Jul 2011 Posts: 54
|
what to do, I can't understand |
Posted: Fri Oct 07, 2011 4:32 am |
|
|
Hello all
I'm new in pic programming.
I want to do:
When push switch 1 pressed, corresponding output enable and the timer run for mentioned time and then output off. Here I used a variable 'p' to assign the timing parameter.
When push switch 2 pressed, corresponding output enable and the timer run for another mentioned time and then output off. Here I used a variable 'q' to assign the timing parameter.
I don't want to use delay because in delay time other instruction of program isn't executed.
Please help and give me suggestion.
Here is my code.
Code: |
#include <16F877A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#include <flex_LCD420.c>
#int_timer0
int16 p,q;
timer0_isr()
{
p++;
q++;
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_4);
enable_interrupts (INT_TIMER0);
enable_interrupts(global);
set_timer0(6);
lcd_init();
// Clear the LCD.
printf(lcd_putc, "\f");
delay_ms(50);
printf(lcd_putc, "\f PIC Experiment");
printf(lcd_putc, "\n--------------------");
printf(lcd_putc, "\nNice Day ");
while(1)
{
if (!input(pin_D0)) // switch 1
{output_high(pin_C0);}
if (p==1000)
{output_low(pin_C0);
p=0;}
if (!input(pin_D1)) // switch 2
{output_high(pin_C1);}
if (q==500)
{output_low(pin_C1);
q=0;}
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Fri Oct 07, 2011 5:01 am |
|
|
First off you don't say what value or range the delays are. Anything from nanoseconds to days is possible with this PIC! So more information is required.
Also since this high level language allows for long variable names, I suggest something more descriptive in place of 'p' and 'q'. It'll help you and others, days or weeks from now, when you're working on this project.
You need some kind of 'debouncing' routine on those 'switches'! Unless you're using expensive electronic switches as mechanical ones are 'noisey'!
Concurrent countdown timers are easy to implement, several ways to do it. Microchip has several examples in their 'Designing for Dollars' applications handbook. As well, CCS kindly supplies example code in the 'examples' folder, that would be useful to you.
And of course the Internet is FULL of code that will do the project you describe. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
freedom
Joined: 10 Jul 2011 Posts: 54
|
|
Posted: Fri Oct 07, 2011 2:31 pm |
|
|
PCM programmer wrote: | You have another thread back in August, where you are working on the
same problem:
http://www.ccsinfo.com/forum/viewtopic.php?t=35429
The problem is that your skill level is still at a "beginner". You must be
at least at an intermediate level to understand this type of program.
|
Dear PCM programmer , above mentioned link the suspected isn't me.
But its true that I'm a beginner.
in this forum, I never expect that someone write my code. I just expect some examples or guidelines.
I'm living in a small town in the country named Bangladesh, nobody here that can help me.
Only some downloaded books and internet and this forum only the way.
I start to learn c by this book "Programming 8-bit PIC Microcontrollers in C
by Martin P.Bates "
If you have a good suggestion on book/tutorial for the beginner like me please help. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Oct 07, 2011 2:48 pm |
|
|
OK, then the first thing you need to do is to fix this:
Quote: |
#int_timer0
int16 p,q;
timer0_isr()
{
p++;
q++;
}
|
You can't put anything between the #int_xxxx statment and the isr
function. If you do that, the isr code will not be compiled (and the
compiler will not tell you this). You can see this in the .LST file, shown
below. Notice there is no ASM code generated:
Code: |
.................... #int_timer0
....................
.................... int16 p,q;
.................... timer0_isr()
.................... {
.................... p++;
.................... q++;
.................... }
....................
.................... void main()
.................... {
|
This is the correct way:
Code: |
int16 p,q;
#int_timer0
timer0_isr()
{
p++;
q++;
}
|
Compile it as above, and notice that you now get ASM code generated:
Code: |
.................... int16 p,q;
....................
.................... #int_timer0
.................... timer0_isr()
.................... {
.................... p++;
*
0037: INCF 28,F
0038: BTFSC 003.2
0039: INCF p+1,F
.................... q++;
003A: INCF 2A,F
003B: BTFSC 003.2
003C: INCF q+1,F
.................... }
....................
003D: BCF 00B.2
003E: BCF 00A.3
003F: BCF 00A.4
0040: GOTO 01B
.................... void main()
.................... {
|
So you need to fix that part of your program, and then you can continue
with the program development. |
|
|
freedom
Joined: 10 Jul 2011 Posts: 54
|
|
Posted: Sat Oct 08, 2011 7:53 am |
|
|
Dear PCM programmer
Thanks a lot for your kind help and guideline.
I start to learn c by this book "Programming 8-bit PIC Microcontrollers in C
by Martin P.Bates".
Do you have have any good suggestion on book/tutorial for the beginner like me please help.
If so, please provide me some link.
Thank you once again. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
freedom
Joined: 10 Jul 2011 Posts: 54
|
|
Posted: Sun Oct 09, 2011 5:50 am |
|
|
thanks a lot |
|
|
freedom
Joined: 10 Jul 2011 Posts: 54
|
|
Posted: Sun Oct 09, 2011 8:35 am |
|
|
Now the code is working but I suspect a problem while I simulate it with Proteus 7.7.
I suspect timing is not accurate as I want to do start the timer at the point that is noted in the code.
what to do:
Code: |
#include <16F877A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#include <flex_LCD420.c>
int16 p,q;
#int_timer0
timer0_isr()
{
p++;
q++;
}
void main()
{
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_4);
enable_interrupts (INT_TIMER0);
enable_interrupts(global);
set_timer0(6);
lcd_init();
// Clear the LCD.
printf(lcd_putc, "\f");
delay_ms(50);
printf(lcd_putc, "\f PIC Experiment");
printf(lcd_putc, "\n--------------------");
printf(lcd_putc, "\nNice Day ");
while(1)
{
if (!input(pin_D0)) // switch 1
{output_high(pin_C0);} // I want to start the timer at this point
if (p==1000)
{output_low(pin_C0);
p=0;}
if (!input(pin_D1)) // switch 2
{output_high(pin_C1);} // I want to start the timer at this point
if (q==500)
{output_low(pin_C1);
q=0;}
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Sun Oct 09, 2011 9:49 am |
|
|
You have two and only Two options.
1) Hack into Proteus and FIX one of the many bugs and errors that it has....
2) Get RID of PROTEUS and use real hardware !
I suspect that option #2 is your only choice. |
|
|
freedom
Joined: 10 Jul 2011 Posts: 54
|
|
Posted: Sun Oct 09, 2011 1:55 pm |
|
|
Thanks temtronic
Actually I suspect there is a bug in my code not in Proteus.
Anyway, in my code is it really timing accurately as per my desire .
Maybe I mistake something. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 09, 2011 5:20 pm |
|
|
My advice is to make a more simple program. Just use one button and
one LED. Get that working reliably. Then add more features later. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Oct 10, 2011 12:29 am |
|
|
Quote: | I want to do start the timer at the point that is noted in the code. |
But you don't. Starting the timer would involve resetting the timer variable. |
|
|
freedom
Joined: 10 Jul 2011 Posts: 54
|
|
Posted: Mon Oct 10, 2011 3:42 am |
|
|
Many thanks FvM for your valued suggestion.
You are right.
Now its working. |
|
|
|