Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

code confusion

Status
Not open for further replies.
i just wanted to ask now the only part left is .the working of the leds.. so .besides interrupts is there another way .. and if there are interrupts ..so i have to initialise the interrupts and .. in the interrupt routine inclide the workign of the leds...

There have been extensive discussions on when to use and not use interrupts. The truth is that you can do it either way. If you revisit the modified code I earlier posted, I placed the servicing for the A/D and the PWM on the interrupts service routine and I put the traffic light LEDs on the regular polling loop.

I prefer to use interrupts only when absolutely necessary. I placed the A/D conversion on the timer2 interrupt service routine so that it is synchronized with the PWM chopping frequency. Here is a link to similar thread that explains that.

https://www.electro-tech-online.com/threads/pic-as-switch-mode-controller.19101/

Regarding the LED traffic light, I had placed it on the regular polling loop. However if you use a software delay loop, the periodic interrupts to service the PWM will introduce an accumulating error in the delay time. A delay set for 5 sec might stretch to longer than that (e.g. 5.5 sec) depending upon the percentage of time spent on the ISR vs. polling loop.

That can be easily solved by a using a hardware timer (timer0 or timer1) which you can regularly poll. You can easily poll the hardware timer faster than once every 1 msec (equivalent to a 5000 instruction program loop). This will introduce a non-accumulating error of less than 1 msec in the 5 second delay.

Of course, you can also place the LED traffic light in the interrupt service as you suggested. If timer2 is used to trigger interrupts, it can be used as fixed time delay which you can count to generate an overall delay of a few seconds for your traffic light.
 
interrupts

so basically if i am not mistaken..

shoudl i shift a2d conversion in timer 2 interrupt and timer 0 interrupt for led ?

do u have an example code .. for the interrupts ?
 
shoudl i shift a2d conversion in timer 2 interrupt and timer 0 interrupt for led ?
If you are going to use interrupts for LED, use timer2 for it also. And so there will be only one interrupt source.

Here is some code taken from a PIC16F877 program. It originally controls two channels of A/D and PWM. I have simplified it for readability.

It uses macros extensively and you have to make your own to suit your application. It does give an outline on how to do it.

Code:
INT_SERVE:
         SAVE_REG
;
         MOVF     ADRESH,W         ; READ ANALOG CHAN 0
         MOVWF    ANCHAN0
;
         CALC_PID 0
;
         START_CONVERT
;
         SET_PWM  0
;
INT_SERVE_END:
         DO_LEDS
;
         BCF      PIR1,TMR2IF
;
         LOAD_REG
;
         RETFIE
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top