POT value using ATmega328P
subratasarkar20
c_cpp
a year ago
3.5 kB
17
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 pot = 0; // Variable to store the ADC result
// LDC interfacing code
#define RS PD0
#define EN PD1
#define LCD_PORT PORTB
char buffer[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 6
OCR0A = 14; // Compare match value for 1ms interval (16MHz clock, 64 prescaler)
TIMSK0 |= (1 << OCIE0A); // Enable Timer 0 Compare Match A Interrupt
}
void adc_init(void)
{
ADMUX |= (1 << REFS0); // Set ADC reference to AVcc
ADMUX |= (1 << MUX0); // Select ADC1 (PC1) input channel
ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC and set prescaler to 128
ADCSRA |= (1 << ADIE); // Enable ADC Interrupt
}
ISR(TIMER0_COMPA_vect)
{
ADCSRA |= (1 << ADSC); // Start ADC conversion on compare match
}
ISR(ADC_vect)
{
pot = 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(buffer, "Pot: %u", pot);
LCD_SetCursor(0, 0);
LCD_String(buffer);
_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