Atmega16 v2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

#include <mega16.

h>
#include <alcd.h>
#include <delay.h>

// define pin configuration


#define KEYPAD_PORT PORTB
#define KEYPAD_DDR DDRB
#define KEYPAD_PIN PINB
#define KEYPAD_ROW_START 4
#define KEYPAD_ROW_END 7
#define KEYPAD_COL_START 0
#define KEYPAD_COL_END 3

#define POT_CHANNEL 7
#define LDR_CHANNEL 0

// global variables
int pot_value = 0;
int ldr_value = 0;

// function prototypes
void read_pot();
void read_ldr();
void display_keypad();

// main function
void main(void) {
// initialize ADC
ADCSRA |= (1 << ADEN); // enable ADC
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // set ADC prescaler to 128

// initialize LCD
lcd_init(20);

// initialize keypad
KEYPAD_DDR |= 0x0F; // set columns as output and rows as input
KEYPAD_PORT |= 0xF0; // enable pull-up resistors on rows

// main loop
while (1) {
read_pot();
read_ldr();
display_keypad();
}
}

// read potentiometer value using ADC


void read_pot() {
ADMUX = (1 << REFS0) | POT_CHANNEL; // set reference voltage to AVCC and select
potentiometer channel
ADCSRA |= (1 << ADSC); // start ADC conversion
while (ADCSRA & (1 << ADSC)); // wait for conversion to complete
pot_value = ADC; // save potentiometer value
}

// read LDR resistance value using ADC


void read_ldr() {
ADMUX = (1 << REFS0) | LDR_CHANNEL; // set reference voltage to AVCC and select LDR
channel
ADCSRA |= (1 << ADSC); // start ADC conversion
while (ADCSRA & (1 << ADSC)); // wait for conversion to complete
ldr_value = ADC; // save LDR value
}

// display keypad input on LCD


void display_keypad() {
char keypad_key = 0;
char keypad_matrix[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

// read keypad input


for (int row = KEYPAD_ROW_START; row <= KEYPAD_ROW_END; row++) {
KEYPAD_PORT |= 0xF0;
KEYPAD_PORT &= ~(1 << row);
for (int col = KEYPAD_COL_START; col <= KEYPAD_COL_END; col++) {
if (!(KEYPAD_PIN & (1 << col))) {
keypad_key = keypad_matrix[row - KEYPAD_ROW_START][col - KEYPAD_COL_START];
break;
}
}
if (keypad_key != 0) {
break;
}
}
// display sensor value based on keypad input
if (keypad_key == 'A') {
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("Pot turn-up:");
lcd_gotoxy(0, 1);
lcd_printf("%4d", pot_value);
}
else if (keypad_key == 'B') {
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("Pot turn-down:");
lcd_gotoxy(0, 1);
lcd_printf("%4d", pot_value);
}
else if (keypad_key == 'C') {
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("LDR resistance:");
lcd_gotoxy(0, 1);
lcd_printf("%4d", ldr_value);
}
else if (keypad_key == 'D') {
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("Keypad input:");
lcd_gotoxy(0, 1);
lcd_putchar(keypad_key);
}
delay_ms(100); // delay for keypad debounce
}
Versi Lain
#include <mega16.h>
#include <stdio.h>
#include <delay.h>
#include <alcd.h>
#include <io.h>
#include <stdlib.h>

// Define keypad pins and values


#define keypad_port PORTB
#define keypad_ddr DDRB
#define keypad_pin PINB
#define keypad_row_pins 0x0F
#define keypad_col_pins 0xF0
#define keypad_col_shift 4

// Define ADC channels


#define pot_channel 7
#define ldr_channel 0

// Define LCD pins


#define lcd_port PORTC
#define lcd_ddr DDRC

void init_adc() {
// Set ADC reference voltage to AVCC
ADMUX |= (1 << REFS0);
// Set ADC result to right-adjusted
ADMUX &= ~(1 << ADLAR);
// Enable ADC and set prescaler to 128
ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
}

unsigned int read_adc(unsigned char channel) {


// Clear ADC channel selection
ADMUX &= 0xF0;
// Select ADC channel
ADMUX |= channel;
// Start ADC conversion
ADCSRA |= (1 << ADSC);
// Wait for ADC conversion to finish
while (ADCSRA & (1 << ADSC)) {};
// Return ADC result
return ADC;
}

char read_keypad() {
// Initialize keypad pins
keypad_ddr = keypad_row_pins | keypad_col_pins;
keypad_port = keypad_row_pins | keypad_col_pins;

// Check each column for key press


for (int col = 0; col < 4; col++) {
// Activate column
keypad_port &= ~(1 << (col + keypad_col_shift));
// Check each row for key press
for (int row = 0; row < 4; row++) {
if (!(keypad_pin & (1 << row))) {
// Deactivate column
keypad_port |= (1 << (col + keypad_col_shift));
// Return key value
return ((row * 4) + col + 1);
}
}
// Deactivate column
keypad_port |= (1 << (col + keypad_col_shift));
}
// Return 0 if no key is pressed
return 0;
}

void main() {
// Initialize ADC and LCD
init_adc();
lcd_init(20);
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("Keypad input:");

while (1) {
// Read input from keypad
char keypad_key = read_keypad();

// Read values from ADC


unsigned int pot_value = read_adc(pot_channel);
unsigned int ldr_value = read_adc(ldr_channel);
// Display values on LCD based on keypad input
if (keypad_key == 'A') {
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("Pot turn-up:");
lcd_gotoxy(0, 1);
lcd_printf("%4d", pot_value);
} else if (keypad_key == 'B') {
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("Pot turn-down:");
lcd_gotoxy(0, 1);
lcd_printf("%4d", pot_value);
} else if (keypad_key == 'C') {
// Display LDR value
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("LDR resistance:");
lcd_gotoxy(0, 1);
lcd_printf("%4d", ldr_value);
} else if (keypad_key == 'D') {
// Display keypad value
lcd_clear();
lcd_gotoxy(0, 0);
lcd_puts("Keypad input:");
lcd_gotoxy(0, 1);
lcd_printf("%c", keypad_key);
}
// Delay to prevent flickering
delay_ms(100);
}
}
Versi Lain Lagi
#include <mega16.h>
#include <stdio.h>
#include <delay.h>
#include <alcd.h>
#include <io.h>
#include <stdlib.h>

#define F_CPU 8000000UL


#define KEYPAD_PORT PORTB
#define KEYPAD_DDR DDRB
#define KEYPAD_PIN PINB

int read_adc(unsigned char adc_input)


{
ADMUX=adc_input | (1<<REFS0);
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC));
return ADCW;
}

void init_adc()
{
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
}

char keypad_get_key()
{
int r, c;
for (r=0; r<4; r++)
{
KEYPAD_PORT = (1 << (r+4));
for (c=0; c<4; c++)
{
if (!(KEYPAD_PIN & (1 << c)))
{
return (r*4+c+1);
}
}
}
return 0;
}

void lcd_show_keypad()
{
lcd_clear();
lcd_puts("Keypad:");
while (1)
{
char key = keypad_get_key();
if (key != 0)
{
lcd_gotoxy(0, 1);
lcd_putc(' ');
lcd_putc(key + '0');
delay_ms(500);
}
}
}

void lcd_show_adc7()
{
char buf[16];
lcd_clear();
while (1)
{
int val = read_adc(7);
lcd_gotoxy(0, 0);
sprintf(buf, "ADC7: %4d", val);
lcd_puts(buf);
delay_ms(500);
}
}

void lcd_show_adc0()
{
char buf[16];
lcd_clear();
while (1)
{
int val = read_adc(0);
lcd_gotoxy(0, 0);
sprintf(buf, "ADC0: %4d", val);
lcd_puts(buf);
delay_ms(500);
}
}

void lcd_show_ldr()
{
char buf[16];
lcd_clear();
while (1)
{
int val = read_adc(0);
float ldr_resistance = (float)val / 1024 * 5000 / val;
lcd_gotoxy(0, 0);
sprintf(buf, "LDR: %.2f Ohm", ldr_resistance);
lcd_puts(buf);
delay_ms(500);
}
}

int main(void)
{
lcd_init(20);
KEYPAD_DDR |= 0x0F;
KEYPAD_PORT |= 0xF0;
init_adc();

while (1)
{
char key = keypad_get_key();
switch (key)
{
case 'A':
lcd_show_adc7();
break;
case 'B':
lcd_show_adc7();
break;
case 'C':
lcd_show_ldr();
break;
case 'D':
lcd_show_keypad();
break;
}
}
}

Penjelasan singkat kode di atas:

 Kita menginisialisasi LCD dan keypad pada fungsi main().


 Fungsi keypad_get_key() akan membaca keypad dan mengembalikan angka keypad
yang ditekan
 Fungsi read_adc() digunakan untuk membaca nilai dari pin ADC tertentu.
 Fungsi init_adc() digunakan untuk menginisialisasi pin ADC.
 Fungsi lcd_show_keypad() akan menampilkan nilai keypad pada layar LCD. Fungsi ini
akan terus berjalan hingga tombol reset ditekan.
 Fungsi lcd_show_adc7() akan menampilkan nilai potensiometer yang terhubung ke pin
ADC7 pada layar LCD. Fungsi ini akan terus berjalan hingga tombol reset ditekan.
 Fungsi lcd_show_adc0() akan menampilkan nilai potensiometer yang terhubung ke pin
ADC0 pada layar LCD. Fungsi ini akan terus berjalan hingga tombol reset ditekan.
 Fungsi lcd_show_ldr() akan menampilkan nilai resistansi LDR yang terhubung ke pin
ADC0 pada layar LCD. Fungsi ini akan terus berjalan hingga tombol reset ditekan.
 Di dalam main(), kita akan terus membaca keypad dan menjalankan fungsi yang sesuai
berdasarkan tombol yang ditekan.

You might also like