|
|
View previous topic :: View next topic |
Author |
Message |
baris_akinci
Joined: 10 Mar 2016 Posts: 17 Location: Ankara
|
timer1 problem |
Posted: Thu Mar 24, 2016 2:38 am |
|
|
Hello, I have been trying to generate SPWM by using Timer1 for two output pins of 16f628a. Actually, I find and optimize this code and test it on real board. It worked well for me.
Code: | #include <16f628a.h>
#FUSES NOWDT, INTRC_IO, NOPROTECT, NOBROWNOUT, NOMCLR, NOCPD
#use delay(clock=4000000)
unsigned int8 pwm_valA=0,pwm_valB=0,pwm_dutyA=0,pwm_dutyB=0;
#int_timer1
void timer1_kesmesi()
{
set_timer1(65500);
if(++pwm_valA<pwm_dutyA) output_high(pin_a0);
else output_low(pin_a0);
if(++pwm_valB<pwm_dutyB) output_high(pin_a1);
else output_low(pin_a1);
}
void main()
{
setup_comparator(NC_NC_NC_NC);
set_tris_a(0X00);
set_timer1(15035);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
enable_interrupts(int_timer1);
enable_interrupts(GLOBAL);
while(TRUE)
{
pwm_dutyA=50;
pwm_dutyB=50;
}
}
|
But if I move this code block into another project as a part of another simulation, the speed of outputs (gradually ON/OFF situation) goes faster, and I can not control it!! Internal OSC and OSC speed are same, prescaler is same, outputs are same!
Please let me know why this happens and how can I control it. Thanks in advance. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Mar 24, 2016 5:33 am |
|
|
re:
if I move this code block into another project as a part of another simulation
Simulation is NOT the same as Real World testing !
First NO 'simulator' actually works 100% properly....
2nd You don't show the 'new' code, only what did work, so we can't 'guess' what is different about version #2.
You need to post your 2nd program for any help.
Jay |
|
|
baris_akinci
Joined: 10 Mar 2016 Posts: 17 Location: Ankara
|
timer1 problem |
Posted: Thu Mar 24, 2016 5:42 am |
|
|
You are right Jay, sorry. here is the second code..
Code: | #include <16F628A.h>
#FUSES NOWDT,NOPROTECT, NOBROWNOUT, NOLVP,NOCPD
#FUSES INTRC_IO
#use fast_io(a)
#use delay(internal=4000000)
#use rs232(baud=2400,xmit=PIN_B2,rcv=PIN_B1,ERRORS)
#priority rda,timer1
#define LED1 pin_a0
#define LED2 pin_a1
#define LED3 pin_a2
#define LED4 pin_a3
char c[20];
char kapat[10];
int counter=1;
int durum=0;
int index = 0;
unsigned int inc=0;
unsigned int8 pwm_valA=0,pwm_valB=0,pwm_dutyA=0,pwm_dutyB=0;
#int_rda
void RDA_isr(void)
{
// rda sub-program, its about the sychronization
}
#int_timer1
void timer1_kesmesi()
{
set_timer1(65500);
output_low(LED1); output_low(LED3);
if(++pwm_valA<pwm_dutyA)output_high(LED2);
else output_low(LED2);
if(++pwm_valB<pwm_dutyB)output_high(LED4);
else output_low(LED4);
}
void sim1() {
int i=0;
output_high(LED1);output_high(LED2); for(i=0;i<70;i++) {delay_ms(1); }
output_low(LED1);output_low(LED2); for(i=0;i<70;i++) {delay_ms(1); }
output_high(LED1);output_high(LED2); for(i=0;i<70;i++) {delay_ms(1); }
output_low(LED1);output_low(LED2); for(i=0;i<70;i++) {delay_ms(1); }
output_high(LED1);output_high(LED2); for(i=0;i<70;i++) {delay_ms(1); }
output_low(LED1);output_low(LED2); for(i=0;i<200;i++) {delay_ms(1); }
output_high(LED3);output_high(LED4); for(i=0;i<70;i++) {delay_ms(1); }
output_low(LED3);output_low(LED4); for(i=0;i<70;i++) {delay_ms(1); }
output_high(LED3);output_high(LED4); for(i=0;i<70;i++) {delay_ms(1); }
output_low(LED3);output_low(LED4); for(i=0;i<70;i++) {delay_ms(1);}
output_high(LED3);output_high(LED4); for(i=0;i<70;i++) {delay_ms(1); }
output_low(LED3);output_low(LED4); for(i=0;i<200;i++) {delay_ms(1); }
}
void sim2() {
int b=0;
output_high(LED1); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED1); for(b=0;b<50;b++){delay_ms(1); }
output_high(LED2); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED2);for(b=0;b<50;b++){delay_ms(1); }
output_high(LED3); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED3);for(b=0;b<50;b++){delay_ms(1); }
output_high(LED4); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED4);for(b=0;b<50;b++){delay_ms(1); }
for(b=0;b<150;b++){delay_ms(1); }
output_high(LED4); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED4);for(b=0;b<50;b++){delay_ms(1); }
output_high(LED3); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED3);for(b=0;b<50;b++){delay_ms(1); }
output_high(LED2); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED2);for(b=0;b<50;b++){delay_ms(1); }
output_high(LED1); for(b=0;b<50;b++){delay_ms(1); }
output_low(LED1);for(b=0;b<50;b++){delay_ms(1); }
}
void sim3() {
int h=0;
for(h=0;h<250;h++){delay_ms(1); }
output_toggle(LED1);output_toggle(LED2);output_toggle(LED3);output_toggle(LED4);
for(h=0;h<250;h++){delay_ms(1); }
output_toggle(LED1);output_toggle(LED2);output_toggle(LED3);output_toggle(LED4);
}
void sim4() {
int a=0;
output_high(LED1); output_high(LED2);output_low(LED3);output_low(LED4);
for(a=0;a<250;a++){delay_ms(1); }
output_low(LED1); output_low(LED2);output_high(LED3);output_high(LED4);
for(a=0;a<250;a++){delay_ms(1); }
}
void sim5() {
int d=0;
output_high(LED1); output_high(LED3);output_low(LED2);output_low(LED4);
for(d=0;d<250;d++){delay_ms(1); }
output_low(LED1); output_low(LED3);output_high(LED2);output_high(LED4);
for(d=0;d<250;d++){delay_ms(1); }
}
void main()
{
set_timer1(15035);
setup_timer_1(T1_INTERNAL| T1_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
enable_interrupts(int_rda);
enable_interrupts(GLOBAL);
set_tris_a(0x00);
while(TRUE)
{
if(inc==1) {
disable_interrupts(INT_TIMER1);
while(inc==1) {sim1();}
enable_interrupts(int_rda);
}
if(inc==2) {
disable_interrupts(INT_TIMER1);
while (inc==2){sim2();}
enable_interrupts(int_rda);
}
if(inc==3) {
disable_interrupts(INT_TIMER1);
while (inc==3){sim3();}
enable_interrupts(int_rda);
}
if(inc==4) {
disable_interrupts(INT_TIMER1);
while (inc==4){sim4();}
enable_interrupts(int_rda);
}
if(inc==5) {
while(inc==5) {
pwm_dutyA=50;
pwm_dutyB=50;
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL); }
enable_interrupts(int_rda);
}
if(inc==6) inc=1;
if(inc==15) { disable_interrupts(INT_TIMER1); output_low(LED1);output_low(LED2);output_low(LED3);output_low(LED4);}
}
}
|
I want to make some LED simulation for fun. all sim() is different to each other and just one simulation remain that is gradually ON/OFF the LEDs!! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Mar 24, 2016 8:43 am |
|
|
comment
1) I can't see where the variable 'inc' ever gets changed. You preset it to zero. In main() all of your if( tests)... always fail as inc=0 .
Jay |
|
|
|
|
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
|