ADC
subratasarkar20
c_cpp
a year ago
3.7 kB
5
Indexable
/*
* SingleADCinputATmega328p.cpp
*
* Created: 17-10-2024 10:34:39
* Author : subrs
*/
#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include "util/delay.h"
#include <stdio.h>
#define TOGGLE_PIN PD2 // Pin to toggle on ADC interrupt
volatile uint16_t x_axis = 0; // Variable to store the ADC result
// LDC interfacing code
#define RS PD0
#define EN PD1
#define LCD_PORT PORTB
char buffer0[16];
char buffer1[16];
char buffer2[16];
void LCD_Command(unsigned char cmnd);
void LCD_Char(unsigned char data);
void LCD_Init(void);
void LCD_String(char *str);
void LCD_Clear(void);
void LCD_SetCursor(unsigned char row, unsigned char col);
void timer0_init(void)
{
// Set Timer 0 to CTC mode
TCCR0A |= (1 << WGM01); // CTC mode
TCCR0B |= (1 << CS01) | (1 << CS00); // Prescaler 64
OCR0A = 15; // Compare match value for 1ms interval (1MHz clock, 64 prescaler)
TIMSK0 |= (1 << OCIE0A); // Enable Timer 0 Compare Match A Interrupt
}
void adc_init(void)
{
ADMUX |= (1 << REFS0); // Set internal 1.1 voltage
ADMUX |= (1 << MUX0); // Select ADC1 (PC1) input channel
ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); // Enable ADC and set prescaler to 16
ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
ADCSRA |= (1 << ADATE); // Enable the auto triggered mode
ADCSRB |= (1 << ADTS1) | (1 << ADTS0); // Auto Trigger Source (Timer0 Compare Match A event as an ADC start trigger)
ADCSRA |= (1 << ADSC);
}
ISR(TIMER0_COMPA_vect)
{
}
ISR(ADC_vect)
{
x_axis = ADC; // Store the 10-bit ADC result
PORTD ^= (1 << TOGGLE_PIN); // Toggle the pin to verify sampling rate
}
int main(void)
{
DDRD |= (1 << TOGGLE_PIN);
timer0_init();
adc_init();
LCD_Init();
sei();
while (1)
{
LCD_Clear();
sprintf(buffer0, "X: %u", x_axis);
LCD_SetCursor(0, 0);
LCD_String(buffer0);
_delay_ms(500);
}
}
// Send a command to the LCD
void LCD_Command(unsigned char cmnd)
{
LCD_PORT = cmnd; // Send command to data bus
PORTD &= ~(1 << RS); // RS = 0 for command
PORTD |= (1 << EN); // Enable pulse
_delay_us(1);
PORTD &= ~(1 << EN);
_delay_ms(2);
}
// Send a character to the LCD
void LCD_Char(unsigned char data)
{
LCD_PORT = data; // Send data to data bus
PORTD |= (1 << RS); // RS = 1 for data
PORTD |= (1 << EN); // Enable pulse
_delay_us(1);
PORTD &= ~(1 << EN);
_delay_ms(2);
}
// Initialize the LCD
void LCD_Init(void)
{
DDRB = 0xFF; // Configure data port for LCD (all output)
DDRD |= (1 << RS) | (1 << EN); // Configure RS and EN pins as output
_delay_ms(20); // LCD power on delay
LCD_Command(0x38); // 8-bit mode, 2-line, 5x7 font
LCD_Command(0x0C); // Display ON, Cursor OFF
LCD_Command(0x06); // Auto-increment cursor
LCD_Command(0x01); // Clear display
_delay_ms(2);
}
// Send a string to the LCD
void LCD_String(char *str)
{
while (*str)
{
LCD_Char(*str++);
}
}
// Clear the LCD display
void LCD_Clear(void)
{
LCD_Command(0x01); // Clear display command
_delay_ms(2);
}
// Set cursor to specific row and column
void LCD_SetCursor(unsigned char row, unsigned char col)
{
unsigned char pos[] = {0x80, 0xC0}; // DDRAM addresses for row 0 and row 1
LCD_Command(pos[row] + col);
}
Editor is loading...
Leave a Comment