;
; pwm_v1.asm
;
; Created: 21/04/2025 13:31:25
; Author : Bemposta
;
.EQU Clock = 16000000 ;processor’s clock frequency, Hz
.EQU Baud = 9600 ;desired serial port baud rate (bits per second)
.EQU UBRRvalue = Clock/(Baud*16) -1 ;calculates value to be put in UBRR0H:L
.DSEG
valorPWM: .byte 1
.CSEG
.ORG 0x00000 ;reset interrupt vector
jmp Start
.ORG URXCaddr ; 0x00024 -> interrupt vectors for USART0
jmp USART0_reception_completed
.ORG INT_VECTORS_SIZE ;leave room for IRQ vectors
Start:
ldi r16, 0xFF
out ddrd, r16
ldi r18, (2<<COM0A0) | (1<<WGM00)
out TCCR0A, r18
ldi r18, (0<<WGM02) | (3<<CS00)
out TCCR0B, r18
ldi r20, 0 ;registro resevado para la PWM. No se debe hacer asi!!!!!!
sts valorPWM, r20
out ocr0a, r20
;configure USART0
RCALL init_USART0
SEI ;enable interrupts globally
Loop:
;nothing to do here, just be alive
NOP
rjmp Loop
init_USART0:
PUSH R16
LDI R16, LOW(UBRRvalue)
STS UBRR0L, R16 ;load the low byte
LDI R16, HIGH(UBRRvalue)
STS UBRR0H, R16 ;load the low byte
; enable receive and transmit, enable USART0 interrupts (UDR empty, Tx finished, Rx finished)
LDI R16, (1<<RXEN0)|(0<<TXEN0)|(0<<UDRIE0)|(0<< TXCIE0)|(1<< RXCIE0)
STS UCSR0B, R16 ;set control register UCSR0B with the corresponding bits
; configure USART 0 as asynchronous, set frame format: 8 data bits, 1 stop bit, no parity
LDI R16, (0<<UMSEL00) |(1<<UCSZ01)|(1<< UCSZ00) |(0<< USBS0)|(0<<UPM01)|(0<< UPM00)
STS UCSR0C, R16 ;set control register UCSR0C with the corresponding bits
POP R16
RET
USART0_reception_completed:
PUSH R16 ;this handler routine will be automatically called every 61msec (in this example)
IN R16, SREG ;Backup SREG. MANDATORY in interrupt handler routines
PUSH R16
push r20
;do the desired periodic task here
LDS R16, UDR0 ;pick up the byte received and do anything with it
cpi r16, 'a'
brne sigue
lds r20, valorPWM
inc r20
out ocr0a, r20
sts valorPWM, r20
sigue:
cpi r16, 's'
brne termina
lds r20, valorPWM
dec r20
out ocr0a, r20
sts valorPWM, r20
out ocr0a, r20
termina:
pop r20
POP R16
OUT SREG, R16 ;Recover SREG from the previous backup
POP R16
RETI ;RETI is MANDATORY when returning from an interrupt handling routine