Skip to content

Instantly share code, notes, and snippets.

@ilsubyeega
Created June 13, 2024 06:45
Show Gist options
  • Save ilsubyeega/eee3b67edb9f83899f26218a37b3701d to your computer and use it in GitHub Desktop.
Save ilsubyeega/eee3b67edb9f83899f26218a37b3701d to your computer and use it in GitHub Desktop.
/*
* LCD + INT + TIMER + ADC
Ports:
- PORTB: OC0 usage
- PORTC: LCD usage (defined in lcd4.h, modified via `#define`)
- PORTD: using INT1
- PORTE: LED-GREEN output, using PE1.
- PORTF: ADC purpose.
Interrupt:
- Enabled
- INT1, falling edge.
Timer:
- Fast PWM, 1024.
- Timer0 overflow interrupt enabled
ADC:
- Interrupt enabled
*/
#include <xc.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>
#include <stdio.h>
#include "lcd4.h"
#define cbi(REG8, BITNUM) REG8 &= ~(_BV(BITNUM))
#define sbi(REG8, BITNUM) REG8 |= _BV(BITNUM)
unsigned volatile char state = 0;
unsigned volatile int ovf_count = 0;
ISR(TIMER0_OVF_vect) {
if (ovf_count > 9) {
ovf_count = 0;
if (state & 0x02) {
PORTE ^= 0x02;
} else {
PORTE = 0x00;
}
} else ovf_count++;
PORTE ^= 0x02;
}
ISR(INT1_vect) {
state ^= 0x01;
if (state & 0x01) {
TCCR0 &= 0xC8; //
TIMSK = 0x00;
PORTE = 0x00;
writeString_lcd4(0, 1, "Listening Stopped");
} else {
TCCR0 |= 0x37;
TIMSK = 0x01;
}
}
long adc_result;
int adc_volt;
char msg[20];
ISR(ADC_vect) {
adc_result = ADC;
adc_volt = adc_result * 5000 / 1023;
sprintf(msg, "val: %d.%03dv", adc_volt/1000, adc_volt%1000);
writeString_lcd4(0, 1, msg);
}
int main(void)
{
// PORTB: Timer OC0 usage
DDRB = 0x10;
// PORTC: LCD Usage
PORTC = 0xff;
// PORTD: using INT1 input
DDRD = 0x00; PORTD = 0x02;
// PORTE: LED-GREEN output, using PE1.
DDRE = 0x02; PORTE = 0x02;
// PORTF: ADC purpose.
DDRF = 0x00;
// Initialize LCD
DDRC = 0xff;
init_lcd4();
writeString_lcd4(0, 0, "Interrupt Mode");
// Initialize INT1 interrupt
EIMSK = 0x02; // INT1
EICRA = 0x08; // falling-edge
// Initialize Timer
TCCR0 = 0x20 // COM01 (HOW)
| 0x48 // Fast PWM
| 0x07; // 1024 clocks
TIMSK = 0x01; // Timer/Counter0 Overflow Interrupt Enable
TCNT0 = 0x00; // reset TCNT0
OCR0 = 0x7f; // 50% compare
// Initialize ADC
ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1);
ADMUX = 0x01; // using on ADC1.
sei(); // set interrupt
while(1) {
ADCSRA |= (1 << ADSC);
_delay_ms(400);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment