Atmega 328

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

ATMEGA 328

dkLED7DOAN
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRB= 0b01111111; //7 chan dieu khien LED 7 doan trang thai xuat

while (1)
{
PORTB= 0b01000000; //sang so 0
delay_ms(500);
PORTB= 0b01111001; //sang so 1
delay_ms(500);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
dkLED7DOAN2
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>

#define thoigian 100


#define PLED PORTB
#define maLed0 0b01000000
#define maLed1 0b01111001

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRB= 0b01111111; //7 chan dieu khien LED 7 doan trang thai xuat

while (1)
{
PLED = maLed0;
delay_ms(thoigian);
PLED = maLed1;
delay_ms(thoigian);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
HamC
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>

unsigned char nutdanhan;

void chop1s(void) //khong truyen tham so


{
//lenh se thuc hien khi goi
PORTB.5 = 1; delay_ms(1000); PORTB.5 = 0;
}

void chop2s(void)
{
PORTB.5 = 1; delay_ms(2000); PORTB.5 = 0;
}

void chopns(unsigned char thamso) //co truyen tham so


{
//lenh se thuc hien khi goi
PORTB.5 = 1; delay_ms((unsigned int)thamso*1000); PORTB.5 = 0;
}

unsigned char kiemtranut(void)


{
unsigned char nuttrave;
if (PINC.3 == 0) {nuttrave = 1;}
else if (PINC.2 == 0) {nuttrave = 2;}
else {nuttrave = 0;}

return nuttrave;
}

unsigned int cong(unsigned char thamso1, unsigned char thamso2)


{
unsigned int giatri;
giatri = thamso1 + thamso2;

return giatri;
}
void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRB.5 = 1; PORTB.5 = 0;
DDRC.2 = 0; PORTC.2 = 1;
DDRC.3 = 0; PORTC.3 = 1;

while (1)
{
nutdanhan = kiemtranut();
if (nutdanhan != 0) {chopns(nutdanhan);}

delay_ms(10);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
input_PCINT
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

unsigned int demngat, demP10, demP11;


unsigned char P10, P11;

// Pin change 8-14 interrupt service routine


interrupt [PC_INT1] void pin_change_isr1(void)
{
demngat++;
if (PINC.2 != P10) {demP10++;}
if (PINC.3 != P11) {demP11++;}

P10 = PINC.2;
P11 = PINC.3;
}

void ht_uchar(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void ht_uint(unsigned int thamso)


{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}
void ht_uintle1(unsigned int thamso)
{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar('.');
lcd_putchar(donvi + 48);
}

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRC.2 = 0; PORTC.2 = 1;
DDRC.3 = 0; PORTC.3 = 1;
P10 = PINC.2;
P11 = PINC.3;

// External Interrupt(s) initialization


// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: On
// Interrupt on any change on pins PCINT16-23: Off
PCICR=(0<<PCIE2) | (1<<PCIE1) | (0<<PCIE0);
PCMSK1=(0<<PCINT14) | (0<<PCINT13) | (0<<PCINT12) | (1<<PCINT11) |
(1<<PCINT10) | (0<<PCINT9) | (0<<PCINT8);
PCIFR=(0<<PCIF2) | (1<<PCIF1) | (0<<PCIF0);

lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("input PCINT");

// Global enable interrupts


#asm("sei")

while (1)
{
delay_ms(100);
lcd_gotoxy(0,1);
ht_uint(demP10);
lcd_gotoxy(8,1);
ht_uint(demP11);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
input_XUNG

//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


#include <mega328.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

unsigned int demngat, demP10, demP11;


unsigned char P10, P11;

// Pin change 8-14 interrupt service routine


interrupt [PC_INT1] void pin_change_isr1(void)
{
demngat++;
if (PINC.2 != P10) {demP10++;}
if (PINC.3 != P11) {demP11++;}

P10 = PINC.2;
P11 = PINC.3;
}

void ht_uchar(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void ht_uint(unsigned int thamso)


{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}
void ht_uintle1(unsigned int thamso)
{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar('.');
lcd_putchar(donvi + 48);
}

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRC.2 = 0; PORTC.2 = 1;
DDRC.3 = 0; PORTC.3 = 1;
P10 = PINC.2;
P11 = PINC.3;

// External Interrupt(s) initialization


// Interrupt on any change on pins PCINT0-7: Off
// Interrupt on any change on pins PCINT8-14: On
// Interrupt on any change on pins PCINT16-23: Off
PCICR=(0<<PCIE2) | (1<<PCIE1) | (0<<PCIE0);
PCMSK1=(0<<PCINT14) | (0<<PCINT13) | (0<<PCINT12) | (1<<PCINT11) |
(1<<PCINT10) | (0<<PCINT9) | (0<<PCINT8);
PCIFR=(0<<PCIF2) | (1<<PCIF1) | (0<<PCIF0);

lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("input PCINT");

// Global enable interrupts


#asm("sei")

while (1)
{
delay_ms(100);
lcd_gotoxy(0,1);
ht_uint(demP10);
lcd_gotoxy(8,1);
ht_uint(demP11);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
inputONOFF
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>

#define thoigian 100


#define PLED PORTB
#define maLedTat 0b01111111
#define maLed0 0b01000000
#define maLed1 0b01111001
#define maLed2 0b00100100

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRB = 0b01111111; //7 chan dieu khien LED 7 doan trang thai xuat
PORTB = 0b01111111;
DDRC.2 = 0; PORTC.2 = 1;
DDRC.3 = 0; PORTC.3 = 1;

while (1)
{
if (PINC.3 == 0) {PLED = maLed1;}
else if (PINC.2 == 0) {PLED = maLed2;}
else {PLED = maLedTat;}

delay_ms(50);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
LCD
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

unsigned char dem;


unsigned int dem2;

void ht_uchar(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void ht_uint(unsigned int thamso)


{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

lcd_init(16);
lcd_gotoxy(5,0);
lcd_putchar('A');
delay_ms(1000);
lcd_putchar('0');

delay_ms(1000);
lcd_gotoxy(0,1);
lcd_putsf("test LCD");

while (1)
{
dem++; dem2 += 50;

lcd_gotoxy(13,0);
ht_uchar(dem);
lcd_gotoxy(11,1);
ht_uint(dem2);

delay_ms(100);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
outputONOFF
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

//DDRB= 0b00100000;
DDRB.5 = 1; //PB5 la chan output

while (1)
{
//PORTB= 0b00100000;
PORTB.5 = 1; //dieu khien chan PB5 muc 1 = LED sang
delay_ms(200);
//PORTB= 0b00000000;
PORTB.5 = 0; //dieu khien chan PB5 muc 0 = LED tat
delay_ms(200);
}
}
input_ANALOG
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

unsigned int kqADC0;

// Voltage Reference: AVCC pin


#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR))

// Read the AD conversion result


unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}

void ht_uchar(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void ht_uint(unsigned int thamso)


{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}
void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRC.0 = 0; PORTC.0 = 0;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: ADC Stopped
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
// ADC4: On, ADC5: On
DIDR0=(0<<ADC5D) | (0<<ADC4D) | (0<<ADC3D) | (0<<ADC2D) | (0<<ADC1D)
| (0<<ADC0D);
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) |
(0<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
ADCSRB=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);

lcd_init(16);

lcd_gotoxy(0,0);
lcd_putsf("input ANALOG");

while (1)
{
kqADC0 = read_adc(0);

lcd_gotoxy(0,1);
ht_uint(kqADC0);

delay_ms(100);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
input_ANALOG2
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

unsigned int kqADC0;


unsigned char phantramVTA, dienapVTA;
// Voltage Reference: AVCC pin
#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR))

// Read the AD conversion result


unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}

void ht_ucharle(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
//lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar('.');
lcd_putchar(donvi + 48);
}

void ht_uchar(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void ht_uint(unsigned int thamso)


{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRC.0 = 0; PORTC.0 = 0;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: ADC Stopped
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
// ADC4: On, ADC5: On
DIDR0=(0<<ADC5D) | (0<<ADC4D) | (0<<ADC3D) | (0<<ADC2D) | (0<<ADC1D)
| (0<<ADC0D);
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) |
(0<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
ADCSRB=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);

lcd_init(16);

lcd_gotoxy(0,0);
lcd_putsf("input ANALOG");

while (1)
{
kqADC0 = read_adc(0);
phantramVTA = (unsigned long int)kqADC0*100/920;
dienapVTA = kqADC0*50/1023; //don vi 0.1V

lcd_gotoxy(0,1);
ht_uint(kqADC0);

lcd_gotoxy(6,1);
ht_uchar(phantramVTA);
lcd_putsf("%");

lcd_gotoxy(11,1);
ht_uchar(dienapVTA);
lcd_putsf("V");

delay_ms(20);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Timer2-dothoigian
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

unsigned char dem, dem2, i;

unsigned int timerdem, thoigianus;

void ht_uchar(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void ht_uint(unsigned int thamso)


{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

TCCR2A = 0b00000000;
TCCR2B = 0b00000111; //so chia 1024

lcd_init(16);

while (1)
{
TCNT2 = 0;

dem++; dem2 += 50;

lcd_gotoxy(0,1);
ht_uchar(dem);
lcd_gotoxy(10,1);
ht_uint(dem2);

timerdem = TCNT2;
thoigianus = timerdem*128;

lcd_gotoxy(0,0);
ht_uint(thoigianus);
lcd_putsf(" us");

//delay_ms(100);

for (i=0;i<3;i++)
{
TCNT2 = 0;
while (TCNT2 < 250) {;}
}

}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
output_PWM
//xxx output PWM xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>

unsigned int kqADC0;

// Voltage Reference: AVCC pin


#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR))

// Read the AD conversion result


unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=(1<<ADSC);
// Wait for the AD conversion to complete
while ((ADCSRA & (1<<ADIF))==0);
ADCSRA|=(1<<ADIF);
return ADCW;
}

void ht_uchar(unsigned char thamso)


{
unsigned char tram, chuc, donvi;
tram = thamso/100; donvi = thamso%10; chuc = thamso/10%10;
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void ht_uint(unsigned int thamso)


{
unsigned char chucngan, ngan, tram, chuc, donvi;
chucngan = thamso/10000;
ngan = thamso/1000%10;
tram = thamso/100%10;
chuc = thamso/10%10;
donvi = thamso%10;
lcd_putchar(chucngan + 48);
lcd_putchar(ngan + 48);
lcd_putchar(tram + 48);
lcd_putchar(chuc + 48);
lcd_putchar(donvi + 48);
}

void main(void)
{

// Crystal Oscillator division factor: 1


#pragma optsize-
CLKPR=(1<<CLKPCE);
CLKPR=(0<<CLKPCE) | (0<<CLKPS3) | (0<<CLKPS2) | (0<<CLKPS1) |
(0<<CLKPS0);
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

DDRC.0 = 0; PORTC.0 = 0;
DDRD.3 = 1;

TCCR2A = 0b00100011; //
TCCR2B = 0b00000100; //so chi 64 fPWM = 488 Hz
OCR2B = 0;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: ADC Stopped
// Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On
// ADC4: On, ADC5: On
DIDR0=(0<<ADC5D) | (0<<ADC4D) | (0<<ADC3D) | (0<<ADC2D) | (0<<ADC1D)
| (0<<ADC0D);
ADMUX=ADC_VREF_TYPE;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIF) | (0<<ADIE) |
(0<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
ADCSRB=(0<<ADTS2) | (0<<ADTS1) | (0<<ADTS0);

lcd_init(16);

lcd_gotoxy(0,0);
lcd_putsf("input ANALOG");

while (1)
{
kqADC0 = read_adc(0);
OCR2B = kqADC0/4;

lcd_gotoxy(0,1);
ht_uint(kqADC0);

delay_ms(100);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You might also like