MaiGoL7
Newbie level 3
- Joined
- Dec 18, 2012
- Messages
- 3
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,314
Hello. I'm working on a project with Atmega8 and I'm switching the code from ASM to C (easier to me), and I have one doubt.
It should send 40times a value via UART when it reaches 1sec.
In assembly I used to work with Timer1 cause I needed fosc/PRE * seconds = 1MHz/256 *1sec= 3906 cycles.
Here is my ASM code:
Switching this code to C I have some issues (Also all this code came from ATmega8 datasheet)
I don't know where to set the prescaler in C code ((1<<CS12)|(0<<CS11)|(0<<CS10)). Nor where to add the counter for send through UART my value 40 times. It should be something like :
But as it's said before, I dont know where to place it.
Also what I thought was add a _delay_ms() that aproaches the timer. But I think may be "better" use this way.
Here is the datasheet and it starts at page 75 16BIT-TIMER/COUNTER1
View attachment atmega8l.pdf
Regards, and merry xmas.
It should send 40times a value via UART when it reaches 1sec.
In assembly I used to work with Timer1 cause I needed fosc/PRE * seconds = 1MHz/256 *1sec= 3906 cycles.
Here is my ASM code:
Code:
Timer1_Init:
LDI R16,(1<<CS12)|(0<<CS11)|(0<<CS10) ;Prescaler 256
OUT TCCR1B,R16
LDI R16,(1<<TOIE1)
OUT TIMSK,R16
LDI R20, 0xF0 ; eTimer1 F0BE (FFFF-0F42) 3906 cicles 1 sec
LDI R21, 0xBE
OUT TR171H, R20
OUT TR171L,R21
Ret
Code:
int_overflow:
IN R1, SREG ; stack
ldi R16, (1<<TXEN) ; TXD UART enabled
out UCSRB, R16
LDI cnt,40 ; send 40 times our value
loop:
LDI R16, 0x38 ; send the value 8 ( HEX )
RCALL USART_Transmit
DEC cnt
BRNE loop
ldi R16, (0<<TXEN) ; turn off TXD UART
out UCSRB, R16
LDI R20, 0xF0 ; TIMER1 at F0BE for 3906 cycles (1 sec)(FFFF-0F42)
LDI R21, 0xBE
OUT TR171H, R20
OUT TR171L,R21
OUT SREG, R1 ; stack
reti
Switching this code to C I have some issues (Also all this code came from ATmega8 datasheet)
Code:
int TIMER_INIT()
{
unsigned int i;
/* Set TCNT1 to 0xF0BE */
TCNT1 = 0xF0BE; // for reach the 3906 cycles
/* Read TCNT1 into i */
i = TCNT1;
}
Code:
unsigned int TIM16_ReadTCNT1( void )
{
unsigned char sreg;
unsigned int i;
/* Save Global Interrupt Flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Read TCNT1 into i */
i = TCNT1;
/* Restore Global Interrupt Flag */
SREG = sreg;
return i;
}
void TIM16_WriteTCNT1( unsigned int i )
{
unsigned char sreg;
unsigned int i;
/* Save Global Interrupt Flag */
sreg = SREG;
/* Disable interrupts */
_CLI();
/* Set TCNT1 to i */
TCNT1 = i;
/* Restore Global Interrupt Flag */
SREG = sreg;
}
I don't know where to set the prescaler in C code ((1<<CS12)|(0<<CS11)|(0<<CS10)). Nor where to add the counter for send through UART my value 40 times. It should be something like :
Code:
for(i=0,i<40,i++){
uart transmit(value);
}
Also what I thought was add a _delay_ms() that aproaches the timer. But I think may be "better" use this way.
Here is the datasheet and it starts at page 75 16BIT-TIMER/COUNTER1
View attachment atmega8l.pdf
Regards, and merry xmas.