Microchips has many feat to offer from tiny lamps to rocketship and mobile harps to brain chip . THis article covers tutorials about understanding cryptic codes for Timer0. Every good and helpfull products deals with Timerx register for modulation purpose, create its own heartbeat of desired signal like the actual ms of time your L.E.D blinks. Products like music effects processor, twangs and notes productions needs Timerx reg (TCCRO and TCCR1). But this time We hack deep but quickie aid of its basics, ISR, binary counters TCCR0 and TCNT0. You can grab comprehensive codes and full gear book in this sitE. AMAZOn_link
First off create your main().
}
Create your data directional(DDRx) for i/o purpose , this determines what port to use and what pins for input and output. You will encounter heavilly in using it as part of magic ":)" of the codes, examples are reading sensors, led, analogpwm, sending signals, cancel outs , frequencies.
Declare "DDRx" and equate to particular bits. This time we will using binaries(0b00000000) for ease representations of each bits. Each bit represents pins of port. 1 means output , 0 means inputs. In this case we only need one lower bit as an output for your L.E.D to blinkblink.
Change x to c for PortC as DDRx to DDRC.
e.g
main() {
DDRC=0b00000001;
}
Access specific bits of port, declare PORTx. Same with the data directory binaries. Change PORTx to PORTC.
e.g
main() {
DDRC=0b00000001;
PORTC=0b00000001;
}
And now, for the niciest part we now tackle Timer0 register and this is the most exiting part to learn. This subject try to explain the nescesary heartbeat time of signals desired like the actual ms of time your L.E.D blinks .
Timer0(TCCR0) formula is very easy and neat , [ 1/ {SystemClock/prescalar } * =< 255 * n = z](z is the actual time blinkblink you need in ms). Prescalar (I use scalar other its original name prescaler) is a registry for subdiving the system clock for the actual millisecond pulse blinkblink you need.
My compiler have complete library it only need to read TCCR0 code and it automates Timer0 and prescalar relations.
TCCR0=0; means not in use , 1 means only system clock, 2 means system clock devided by 8, 3 = system clock/64, 4=sysclk/256, 5=sysClk/1024.
We need at this time prescalar 8, we declare as TCCR0=2, or TCCR0=0b00000010 in bin.
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001
TCCR0=2;
}
In this code we need .2ms of blinkblink and our microcontroler is set to 6mhz system clock as only given example for this time.
For automation, set TCCRO reg to 31 (TCCR0=31) the diffrence of 255-x=31 .Taken from the formula of "[ {1/(6/8)} * (y * n = z)]".
y = <= 255 and z = actual time you blinkblink your L.E.D ouputed in your PORTC pin .
By multiplying the minuend (the y variable) to n logically you can obtain the "z" ms blinkblink you need . So declare TCNT0=minuend.
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001;
TCCR0=2;
TCNT0=31;
}
Enable global interrupts(isr).
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001;
TCCR0=2;
TCNT0=31;
#asm("sei")
}
Set infinite while loops.
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001;
TCNT0=2;
TCCR0=31;
#asm("sei")
while(1) {
;
}
}
Lastly declare interrupts(isr) but above before the main method. This isr is use heavily for countin time only thus overflow or ovf.
e.g
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
}
Cast again the "reload number" or the minuend.
e.g
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
TCNT0=31;
}
COMPLETE CODE
#include <MEGAx.h>
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
TCNT0=31;
}
void main(void){
DDRC=0b00000001;
PORTC=0b00000001;
TCNT0=2;
TCCR0=31;
#asm("sei")
while(1) {
;
}
}
GRAB YOUR COMPILER HERE . purchaseLInk
I code in Greek. |
First off create your main().
e.g
void main (void) {}
Create your data directional(DDRx) for i/o purpose , this determines what port to use and what pins for input and output. You will encounter heavilly in using it as part of magic ":)" of the codes, examples are reading sensors, led, analogpwm, sending signals, cancel outs , frequencies.
Declare "DDRx" and equate to particular bits. This time we will using binaries(0b00000000) for ease representations of each bits. Each bit represents pins of port. 1 means output , 0 means inputs. In this case we only need one lower bit as an output for your L.E.D to blinkblink.
Change x to c for PortC as DDRx to DDRC.
e.g
main() {
DDRC=0b00000001;
}
Access specific bits of port, declare PORTx. Same with the data directory binaries. Change PORTx to PORTC.
e.g
main() {
DDRC=0b00000001;
PORTC=0b00000001;
}
And now, for the niciest part we now tackle Timer0 register and this is the most exiting part to learn. This subject try to explain the nescesary heartbeat time of signals desired like the actual ms of time your L.E.D blinks .
Timer0(TCCR0) formula is very easy and neat , [ 1/ {SystemClock/prescalar } * =< 255 * n = z](z is the actual time blinkblink you need in ms). Prescalar (I use scalar other its original name prescaler) is a registry for subdiving the system clock for the actual millisecond pulse blinkblink you need.
My compiler have complete library it only need to read TCCR0 code and it automates Timer0 and prescalar relations.
TCCR0=0; means not in use , 1 means only system clock, 2 means system clock devided by 8, 3 = system clock/64, 4=sysclk/256, 5=sysClk/1024.
We need at this time prescalar 8, we declare as TCCR0=2, or TCCR0=0b00000010 in bin.
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001
TCCR0=2;
}
In this code we need .2ms of blinkblink and our microcontroler is set to 6mhz system clock as only given example for this time.
For automation, set TCCRO reg to 31 (TCCR0=31) the diffrence of 255-x=31 .Taken from the formula of "[ {1/(6/8)} * (y * n = z)]".
y = <= 255 and z = actual time you blinkblink your L.E.D ouputed in your PORTC pin .
By multiplying the minuend (the y variable) to n logically you can obtain the "z" ms blinkblink you need . So declare TCNT0=minuend.
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001;
TCCR0=2;
TCNT0=31;
}
Enable global interrupts(isr).
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001;
TCCR0=2;
TCNT0=31;
#asm("sei")
}
Set infinite while loops.
e.g
main(){
DDRC=0b00000001;
PORTC=0b00000001;
TCNT0=2;
TCCR0=31;
#asm("sei")
while(1) {
;
}
}
Lastly declare interrupts(isr) but above before the main method. This isr is use heavily for countin time only thus overflow or ovf.
e.g
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
}
Cast again the "reload number" or the minuend.
e.g
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
TCNT0=31;
}
COMPLETE CODE
#include <MEGAx.h>
interrupt [TIM0_OVF] void timer0_ovf_isr(void) {
TCNT0=31;
}
void main(void){
DDRC=0b00000001;
PORTC=0b00000001;
TCNT0=2;
TCCR0=31;
#asm("sei")
while(1) {
;
}
}
GRAB YOUR COMPILER HERE . purchaseLInk