micro basic error with timer1 interrupt

Thread Starter

h.d

Joined Oct 22, 2007
150
hi all
am working in the following program and has two problems
i will talk about what th eprogram do and the problems
and please help me to solve it
first it for PIC 16F877A 4Mhz

its 5 inputs
RD2 FOR INCREASE SPEED
RD3 DOR DECREASE SPEED
RD4 TO START PROGRAM
RB0 (INTERRUPT)STOP PWM AND WHOLE PROGRAM THEN DISPLAY "STOP"
RA4 ENXTERNAL COUNTER

IN ADITION TO TIMER SECOUNDES AND MINUETES START WITH THE PROGRAM AND DISPLAYED ON LCD

THE SPEED AND DISTANCE DISPALYED ON LCD ..
THE PROBLEMS
1. WHEN INTERRUPT RB0 CANT USE THE DISPLAY AND I DONT KNOW HOW TO DO IT
2. FOR THE TIMER ITS LAG 18SEC FROM THE PROGRAM START AND IF PRESS OUN RD2 AND RD3 THE TIMER STOP AND LAG 18 SEC


SO PEASE HELP ME TO SOLVE THAT...​



Code:


Rich (BB code):
program functiontestpwm 
dim z1 as byte 
dim cnt as byte 

sub procedure delay800    'delay 800ms 
delay_ms(800) 
end sub 

sub procedure delay100    'delay 100ms 
delay_ms(100) 
end sub 




sub procedure interrupt 
if INTCON.INTF=1 then 
intcon.intf=0 
pwm_stop 
end if 
  cnt = cnt + 1 
  pir1.0 = 0      ' Clear TMR1IF 
end sub 


sub function upspeed(dim x as byte) as byte  'increase speed 
dim j as byte 
j=x+10 
if j>190 then 
j=190 
end if 
result=j 
end sub 

sub function downspeed(dim r as byte)as byte      'decrease speed 
dim f as byte 
f=r-10 
if f>200 then 
f=0 end if 
result=f 
end sub 



dim sec,min,a as byte            'decleration 
dim min1,sec1 as string[3] 

dim y,b,w,q,dist,se as byte 
dim y1,q1,dist1 as string[3] 

main: 
b=1 
z1=0 
q=0 
w=0 
dist=0 
tmr0=0 

T1CON = 1 
PIR1.TMR1IF = 0         ' Clear TMR1IF 
PIE1  =   1             ' Enable interrupts 

cnt =   0               ' Initialize cnt 


option_reg=$68 
intcon=$D0    'timer1 and RB0 interrupt 

trisd=$ff 
trisb=$00 
trisa=$ff 
portc=0 

lcd_config(portb,7,6,5,4,portb,1,2,3)'display primary parameters 
delay800 
lcd_cmd(lcd_cursor_off) 
lcd_out(1,1,"wait start.") 


start: 

delay800 
lcd_cmd(lcd_clear) 
delay800 
lcd_out(1,1,"time  spd  dist.")   'dispay promary parameters 


pwm_init(8000)'pwm initiat 
delay100 
pwm_start 'start pwm D.C=0 
run: 

'If cnt is 15, then inc sec untill =60 inc min and reset cnt 

if cnt = 15 then  'timer 
      inc(sec) 

      bytetostr(sec,sec1) 

      lcd_out(2,3,sec1) 
      cnt = 0 

if sec=59 then 
      inc(min) 
      sec=0 
      end if 
    end if 

    bytetostr(min,min1) 
    lcd_out(2,0,min1) 


if portd.2=0 then'up-speed switch 
delay800 
z1=upspeed(z1) 

end if 

if portd.3=0 then   'down speed switch 
delay800 
z1=downspeed(z1) 


end if 

 pwm_change_duty(z)'change speed 


bytetostr(z1,y1) 
lcd_out(2,8,y1)  'display speed 


if tmr0=5 then   'shaft encoder signal to calvulate distance 
 inc(q) 
 tmr0=0 
 end if 
  bytetostr(q,q1) 
 lcd_out(2,12,q1) 


 goto run 


end.
 
Last edited:

hgmjr

Joined Jan 28, 2005
9,027
Rich (BB code):
Code: 
 
program functiontestpwm 
dim z1 as byte 
dim cnt as byte 
 
sub procedure delay800 'delay 800ms 
   delay_ms(800) 
end sub 
 
sub procedure delay100 'delay 100ms 
   delay_ms(100) 
end sub 
 
 
 
 
sub procedure interrupt 
   if INTCON.INTF=1 then 
      intcon.intf=0 
      pwm_stop 
   end if 
   cnt = cnt + 1 
   pir1.0 = 0 ' Clear TMR1IF 
end sub 
 
 
sub function upspeed(dim x as byte) as byte 'increase speed 
   dim j as byte 
   j=x+10 
   if j>190 then 
      j=190 
   end if 
   result=j 
end sub 
 
sub function downspeed(dim r as byte)as byte 'decrease speed 
   dim f as byte 
   f=r-10 
   if f>200 then 
      f=0 
   end if 
   result=f 
end sub 
 
 
 
dim sec,min,a as byte 'decleration 
dim min1,sec1 as string[3] 
 
dim y,b,w,q,dist,se as byte 
dim y1,q1,dist1 as string[3] 
 
main: 
   b=1 
   z1=0 
   q=0 
   w=0 
   dist=0 
   tmr0=0 
 
   T1CON = 1 
   PIR1.TMR1IF = 0 ' Clear TMR1IF 
   PIE1 = 1 ' Enable interrupts 
 
   cnt = 0 ' Initialize cnt 
 
 
   option_reg=$68 
   intcon=$D0 'timer1 and RB0 interrupt 
 
   trisd=$ff 
   trisb=$00 
   trisa=$ff 
   portc=0 
 
   lcd_config(portb,7,6,5,4,portb,1,2,3)'display primary parameters 
   delay800 
   lcd_cmd(lcd_cursor_off) 
   lcd_out(1,1,"wait start.") 
 
 
start: 
 
   delay800 
   lcd_cmd(lcd_clear) 
   delay800 
   lcd_out(1,1,"time spd dist.") 'dispay promary parameters 
 
 
   pwm_init(8000)'pwm initiat   
   delay100 
   pwm_start 'start pwm D.C=0 
 
run: 
 
   'If cnt is 15, then inc sec untill =60 inc min and reset cnt 
 
   if cnt = 15 then 'timer 
      inc(sec) 
 
      bytetostr(sec,sec1) 
 
      lcd_out(2,3,sec1) 
      cnt = 0 
 
      if sec=59 then 
         inc(min) 
         sec=0 
      end if 
   end if 
 
   bytetostr(min,min1) 
   lcd_out(2,0,min1) 
 
 
   if portd.2=0 then'up-speed switch 
      delay800 
      z1=upspeed(z1) 
 
   end if 
 
   if portd.3=0 then 'down speed switch 
      delay800 
      z1=downspeed(z1) 
 
 
   end if 
 
   pwm_change_duty(z)'change speed 
 
 
   bytetostr(z1,y1) 
   lcd_out(2,8,y1) 'display speed 
 
 
   if tmr0=5 then 'shaft encoder signal to calvulate distance 
      inc(q) 
      tmr0=0 
   end if 
   bytetostr(q,q1) 
   lcd_out(2,12,q1) 
 
   goto run 
 
end.
Formatted the code from your original post using the "CODE" tags.
 
Last edited:
Top