Atmega 328
Atmega 328
Atmega 328
dkLED7DOAN
//xxx in put ON OFF xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#include <mega328.h>
#include <delay.h>
void main(void)
{
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>
void main(void)
{
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>
void chop2s(void)
{
PORTB.5 = 1; delay_ms(2000); PORTB.5 = 0;
}
return nuttrave;
}
return giatri;
}
void main(void)
{
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>
P10 = PINC.2;
P11 = PINC.3;
}
void main(void)
{
DDRC.2 = 0; PORTC.2 = 1;
DDRC.3 = 0; PORTC.3 = 1;
P10 = PINC.2;
P11 = PINC.3;
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("input PCINT");
while (1)
{
delay_ms(100);
lcd_gotoxy(0,1);
ht_uint(demP10);
lcd_gotoxy(8,1);
ht_uint(demP11);
}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
input_XUNG
P10 = PINC.2;
P11 = PINC.3;
}
void main(void)
{
DDRC.2 = 0; PORTC.2 = 1;
DDRC.3 = 0; PORTC.3 = 1;
P10 = PINC.2;
P11 = PINC.3;
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("input PCINT");
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>
void main(void)
{
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>
void main(void)
{
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)
{
//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>
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>
void main(void)
{
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>
void main(void)
{
TCCR2A = 0b00000000;
TCCR2B = 0b00000111; //so chia 1024
lcd_init(16);
while (1)
{
TCNT2 = 0;
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>
void main(void)
{
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