Tổng hợp về đồng hồ vạn niên , lịch cho 16f877a và 8051
Link tải : https://drive.google.com/open?id=1cFRj2mYjXPFyVJz-4q2WuXDUL5xdi7xw
Read More
Link tải : https://drive.google.com/open?id=1cFRj2mYjXPFyVJz-4q2WuXDUL5xdi7xw
#define data_pin PIN_B0
#define clock_pin PIN_B1
#define latch_pin PIN_B2
#include <16f877a .h="">
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 8000000)
#use fast_io(B) // dinh che do io thu cong
#use fast_io(D)
/*khai bao ham con*/
void cauhinhport();
unsigned int seg(unsigned int num);
void kiemtranutnhan();
void write_data(unsigned int number);
void quetled7doan();
/*khai bao bien toan cuc*/
short s;
unsigned int j,nghin,tram,chuc,donvi ;
unsigned long i = 0;
/*ham con chau hinh port*/
void cauhinhport()
{
port_b_pullups(TRUE); // bat dien tro keo port B
output_b(0); // PORTB khoi tao cong
set_tris_b(0x18); // Cau hinh RB3 & RB4 nhu la input
output_d(0); // PORTD khoi tao cong
set_tris_d(0);
}
/*ham con hien thi led 7 doan anode chung*/
unsigned int seg(unsigned int num) {
switch (num) {
case 0 : return 0xc0;
case 1 : return 0xf9;
case 2 : return 0xa4;
case 3 : return 0xb0;
case 4 : return 0x99;
case 5 : return 0x92;
case 6 : return 0x82;
case 7 : return 0xF8;
case 8 : return 0x80;
case 9 : return 0x90;
}
}
/*ham con dich bit data*/
void write_data(unsigned int number){
for(j = 0x80; j > 0; j = j >> 1)
{
if(number & j){
output_high(data_pin); // xung cao
}
else
{
output_low(data_pin); //xung thap
}
/*tao xung clock day data*/
output_high(clock_pin);
output_low(clock_pin);
}
/*tao xung ket thuc day data*/
output_high(latch_pin);
output_low(latch_pin);
}
/*ham con kiem tra nut nhan*/
void kiemtranutnhan()
{
if(input(PIN_B3) && input(PIN_B4))
s = 1;
if(s == 1) {
if(input(PIN_B3) == 0) {
s = 0;
i++;
if(i > 9999)
i = 0;
}
if(input(PIN_B4) == 0) {
s = 0;
if(i < 1)
i = 1;
i--;
}
}
nghin = seg(i/1000); // hang nghin
tram = seg((i %1000) /100); // hang tram
chuc = seg(((i %1000)%100)/10); // hang chuc
donvi = seg(i% 10); // hang don vi
}
void quetled7doan()
{
/*quet led thu nhat*/
output_high(PIN_D0);
write_data(nghin);
delay_ms(1);
output_low(PIN_D0);
/*quet led thu hai*/
output_high(PIN_D1);
write_data(tram);
delay_ms(1);
output_low(PIN_D1);
/*quet led thu ba*/
output_high(PIN_D2);
write_data(chuc);
delay_ms(1);
output_low(PIN_D2);
/*quet led thu tu*/
output_high(PIN_D3);
write_data(donvi);
delay_ms(1);
output_low(PIN_D3);
}
/*ham chinh xu ly*/
void main(){
cauhinhport(); // Cau hinh port nhu la output
while(TRUE)
{
/* kiem tra phim nhat*/
kiemtranutnhan();
/*tien hanh quet led*/
quetled7doan();
}
}
16f877a>
#include <16f887 .h="">
#fuses NOMCLR, NOBROWNOUT, NOLVP, INTRC_IO
#use delay(clock = 4MHz)
#use fast_io(B)
#use rtos(timer = 0, minor_cycle = 50ms)
#task(rate = 250ms, max = 50ms) // 1st RTOS task (executed every 250ms)
void led1(){
output_toggle(PIN_B0);
}
#task(rate = 500ms, max = 50ms) // 2nd RTOS task (executed every 500ms)
void led2(){
output_toggle(PIN_B1);
}
#task(rate = 750ms, max = 50ms) // 3rd RTOS task (executed every 750ms)
void led3(){
output_toggle(PIN_B2);
}
#task(rate = 1000ms, max = 50ms) // 4th RTOS task (executed every 1000ms)
void led4(){
output_toggle(PIN_B3);
}
#task(rate = 1250ms, max = 50ms) // 5th RTOS task (executed every 1250ms)
void led5(){
output_toggle(PIN_B4);
}
void main(){
setup_oscillator(OSC_4MHZ); // Set the internal oscillator to 4MHz
output_b(0); // All PORTB register pins are zeros
set_tris_b(0); // Configure PORTB pins as outputs
rtos_run(); // bắt đầu chạy rtos
}
16f887>
#include "main.h"
int16 i,j,n,m,k=0,l=0;
void main()
{
setup_adc(ADC_CLOCK_DIV_32); // chon bo chia ADC
setup_adc_ports(ALL_ANALOG); // mo tat cac cong ANALOG
setup_ccp1(CCP_PWM); // cai dat ccp1
setup_ccp2(CCP_PWM); // cai dat ccp1
delay_ms(100); // cho 100ms de hoan tat
setup_timer_2(T2_DIV_BY_16,255, 1); // cai dat de lay mau dua vao cong thuc
// truong hop co loc trung binh
while(false)
{
set_adc_channel(0); // chon kenh dau tien
delay_ms(1);
i=0; j=0; // tao gia tri mac dinh
for(n=0;n<50 50="" adc="" ap="" bien="" binh="" chia="" chon="" cong="" cua="" cycle="" dau="" de="" delay_ms="" dien="" duty="" for="" gan="" gia="" hop="" i="" j="" k="" kenh="" khong="" l="" lam="" lan="" loc="" m="0;m<50;m++)" n="" pre="" set_adc_channel="" set_pwm1_duty="" set_pwm2_duty="" tam="" tien="" toff="" ton="" tri="" true="" trung="" truong="" va="" vao="" while="">
50>
#include "main.h"Đo dạng sóng của analog :
#define led1 PIN_D0
#define led2 PIN_D1
#define led3 PIN_D2
void main()
{
while(1)
{
int16 t1=0,t2=0,t3=0,z;
for(z=0;z<350 delay_ms="" if="" t1="" t2="" t3="" z="">10)
{
output_toggle(led1);
t1=0;
}
if(t2>100)
{
output_toggle(led2);
t2=0;
}
if(t3>=350)
{
output_toggle(led3);
t3=0;
}
}
}
}
350>
#include <main.h>
#include <lcd4bit.c>
unsigned char Code7Seg[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};
int16 kqadc,donvi,chuc;
void hienthiadcva7doan()
{
set_tris_d(0x00);
set_tris_a(0x00);
set_adc_channel(0);
setup_adc(adc_clock_div_2);
setup_adc_ports(AN0);
kqadc=0;
for(int i=0;i<200;i++)
{
kqadc=kqadc+read_adc();
delay_ms(1);
}
kqadc=kqadc/2.046;
kqadc=kqadc/200;
chuc =kqadc/10;
donvi=kqadc%10;
output_high(PIN_C0);
output_B(code7seg[chuc]);
delay_ms(1);
output_low(PIN_C0);
output_high(PIN_C1);
output_B(code7seg[donvi]);
delay_ms(1);
output_low(PIN_C1);
}
void canhbaonhietdo()
{
output_high(PIN_C2);
}
void main()
{
LCD_Init();
while(TRUE)
{
hienthiadcva7doan();
if(kqadc>=32)
{
canhbaonhietdo();
LCD_PutCmd(0x80);
LCD_PutChar("Nhiet do cao !!!");
}
else
{
output_low(PIN_C2);
LCD_PutCmd(0x80);
LCD_PutChar("Nhiet do : ");
}
LCD_PutCmd(0xc0);
WriteNum(kqadc);
}
}
#include <main.h>Link project : LM35 và PIC 877a
#include "lcd4bit.c"
#Fuses HS,NOWDT,NOPROTECT,NOLVP
#Use delay(clock=20000000)
int16 kqadc;
void caidatadc()
{
SET_TRIS_D(0X00);
SET_TRIS_A(0X00);
SETUP_ADC(ADC_CLOCK_DIV_2);
SETUP_ADC_PORTS(AN0);
SET_ADC_CHANNEL(0);
kqadc=0;
for(int i=0;i<200;i++)
{
KQADC=kqadc+READ_ADC();
delay_ms(1);
}
kqadc=kqadc/2.046;
kqadc=kqadc/200;
}
Void main() // Chuong trinh chinh
{
caidatadc();
Lcd_init(); // Khoi dong che do hoat dong cua LCD
Delay_ms(20);// Cho de LCD hoat dong on dinh
While (TRUE)
{
Lcd_putcmd(0x80); // Bat dau hien thi tai vi tri thu 2 dong 1
Lcd_putchar("Nhiet Do:");
Lcd_putcmd(0xC0); // Bat dau hien thi tai vi tri thu 1 dong 2
WriteNum(kqadc);
}
}
#include "stddef.h"
#define LCD_RS PIN_D2
#define LCD_EN PIN_D3
#define LCD_D4 PIN_D4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7
// misc display defines-
#define Line_1 0x80
#define Line_2 0xC0
#define Clear_Scr 0x01
// prototype statements
#separate void LCD_Init(void); //Ham khoi tao LCD
#separate void LCD_SetPosition(unsigned int cX);//Thiet lap vi tri con tro
//Ham viet 1ky tu hoac 1 chuoi len LCD
#separate void LCD_PutChar(unsigned int cX);
#separate void LCD_PutCmd(unsigned int cX) ; //Ham gui lenh len LCD
#separate void LCD_PulseEnable(void); //Xung kich hoat
#separate void LCD_SetData(unsigned int cX); //Dat du lieu len chan Data
#separate void WriteNum(unsigned char num);
#separate void HTsothapphan(Int32 num);
// Dinh nghia Cong
#use standard_io(B)
#use standard_io(A)
//Khoi tao LCD
#separate void LCD_Init (void)
{
LCD_SetData (0x00);
Delay_ms(20); // Cho cho LCD khoi tao >> 15ms
output_low(LCD_RS); // Che do gui lenh
LCD_SetData(0x03); // Khoi tao che do 4 bit
LCD_PulseEnable();
LCD_SetData(0x02); // Tao giao dien 4 bit
LCD_PulseEnable(); // send dual nibbles hereafter, MSN first
LCD_PutCmd(0x2C); // function set (all lines, 5x7 characters)
LCD_PutCmd(0x0C); // display ON, cursor off, no blink
LCD_PutCmd(0x06); // entry mode set, increment & scroll left
LCD_PutCmd(0x01); // clear display
}
#separate void LCD_SetPosition(unsigned int cX)
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData(swap(cX)|0x08);
LCD_PulseEnable();
LCD_SetData(swap(cX));
LCD_PulseEnable();
}
#separate void LCD_PutChar(unsigned int cX)
{
/* this subroutine works specifically for 4-bit Port A */
output_high(LCD_RS);
LCD_PutCmd(cX);
output_low(LCD_RS);
}
#separate void LCD_PutCmd(unsigned int cX)
{
/* this subroutine works specifically for 4-bit Port A */
LCD_SetData(swap(cX)); /* send high nibble */
LCD_PulseEnable();
LCD_SetData(swap(cX)); /* send low nibble */
LCD_PulseEnable();
}
#separate void LCD_PulseEnable(void)
{
output_high(LCD_EN);
delay_us(5);
output_low(LCD_EN);
delay_ms(5);
}
#separate void LCD_SetData(unsigned int cX)
{
output_bit(LCD_D4, cX & 0x01);
output_bit(LCD_D5, cX & 0x02);
output_bit(LCD_D6, cX & 0x04);
output_bit(LCD_D7, cX & 0x08);
}
#separate void WriteNum(unsigned char num)
{
unsigned char digit;
digit = num/100;
LCD_PutChar(digit + '0'); //write tram+ma ASCII cua '0'
digit = num/10;
digit = digit % 10;
LCD_PutChar(digit + '0'); //write chuc
digit = num % 100;
digit = digit % 10;
LCD_PutChar(digit + '0'); //write dvi
}
#separate void HTsothapphan(Int32 num)
{
Int8 Integer, First, Second, Third;
Integer = num/1000 + 0x30; // Tach phan nguyen
num= num%1000;
First = num/100 + 0x30; // Tach phan le thu 1
num = num%100;
Second = num/10 + 0x30; // Tach phan le thu 2
Third = num%10 + 0x30; // Tach phan le thu 3
Lcd_putcmd(0xCA);
Lcd_putchar(Integer);
Lcd_putchar(".");
Lcd_putchar(First);
Lcd_putchar(Second);
Lcd_putchar(Third);
}
<8 i="" move="move">#include <main.h>
UNSIGNED INT16 KQADC,SOV=5,SON=1023;
// SANG DAN 8 LED
void SANG_DAN(VOID)
{
unsigned int8 init=0x7F, temp, reset =0xff, move;
output_D(reset);
delay_MS(1000);
move=init;
output_D(move);
delay_MS(1000);
for(int8 i=0;i<8;i++)
{
move=move>>1;
temp=move;
output_D(temp);
delay_MS(1000);
}
}
// NHAY 8 LED CHU KY 0.5 ( TAT 0.25 VA MO 0.25)
VOID NHAY05GIAY(VOID)
{
OUTPUT_D(0X00);
DELAY_MS(250);
OUTPUT_D(0XFF);
DELAY_MS(250);
}
// SANG TAT 8 LED PORTD
VOID SANG_TAT()
{
OUTPUT_D(0X00);
DELAY_MS(500);
OUTPUT_D(0XFF);
DELAY_MS(500);
}
// DICH 8 LED
VOID DICH8LED(VOID)
{
output_D(0b01111111);
delay_MS(1000);
output_D(0b10111111);
delay_MS(1000);
output_D(0b11011111);
delay_MS(1000);
output_D(0b11101111);
delay_MS(1000);
output_D(0b11110111);
delay_MS(1000);
output_D(0b11111011);
delay_MS(1000);
output_D(0b11111101);
delay_MS(1000);
output_D(0b11111110);
delay_MS(1000);
output_D(0b11111111);
delay_MS(1000);
}
VOID HIENTHI()
{
KQADC=SOV*READ_ADC()/SON;
//NUT NHAN 1
IF(KQADC==4)
{
// SANG TAT 8 LED PORTD
SANG_TAT();
}
// NUT NHAN 2
ELSE IF(KQADC==3)
{
// SANG DAN 8 LED PORTd
SANG_DAN();
}
//NUT NHAN 3
ELSE IF(KQADC==2)
{
// DICH 8 LED PORTd
DICH8LED();
}
// NUT NHAN 4
ELSE IF(KQADC==1)
{
// NHAY 8 LED PORTd CHU KY 0.5S
NHAY05GIAY();
}
ELSE
OUTPUT_D(0XFF);
}
void main()
{
SET_TRIS_D(0X00);
SET_TRIS_A(0X00);
SETUP_ADC(ADC_CLOCK_DIV_2);
SETUP_ADC_PORTS(AN0);
SET_ADC_CHANNEL(0);
while(TRUE)
{
HIENTHI();
}
}
8>
#include
#byte congatRB = 6
#INT_RB
void ngatRB()
{
// quet nut nhan
if(NUTNHAN_1==0)
{
while(NUTNHAN_1==0)
{
int8 clear;
output_toggle(LED1);
delay_ms(500);
clear= congatRB;
}
}
if(NUTNHAN_4==0)
{
while(NUTNHAN_4==0)
{
int8 clear;
output_toggle(LED4);
delay_ms(500);
clear= congatRB;
}
}
if(NUTNHAN_2==0)
{
while(NUTNHAN_2==0)
{
int8 clear;
output_toggle(LED2);
delay_ms(500);
clear= congatRB;
}
}
if(NUTNHAN_3==0)
{
while(NUTNHAN_3==0)
{
int8 clear;
output_toggle(LED3);
delay_ms(500);
clear= congatRB;
}
}
}
void main()
{
//xoa ngat RB
clear_interrupt(INT_RB);
// Bat ngat RB
enable_interrupts(INT_RB);
// Cho phep ngat toan cuc
enable_interrupts(GLOBAL);
// tat tac ca cac LED
output_bit(LED1,0);
output_bit(LED2,0);
output_bit(LED3,0);
output_bit(LED4,0);
while(TRUE)
{
}
}
#include
unsigned int x=0x00;
void main()
{
set_tris_D(0x00);
setup_timer_1(t1_internal|t1_div_by_8);
set_timer1(3036); // 65536-3036 =62500 , 62500*1.6= 100000 =100ms
while(TRUE)
{
if(tmr1if==1)
{
x=~x;
output_D(x);
set_timer1(3036);
tmr1if=0;
}
}
}
/* dieu khien cac led don sang tat dung timer 1 voi chu ky delay la 210ms */
/*
thuat toan nhu sau T=210ms vay Ton=Toff=105ms
dung he so chia 8 va port D xuat va bat dau dem timer
*/
#include
unsigned int8 X=0x00;
void main()
{
SET_TRIS_D(0x00);
setup_timer_1(T1_internal| T1_div_by_8);
set_timer1(0); // dem bat dau tu 0
while (true)
{
if (tmr1if==1)
{
X=~X;
output_D(x);
tmr1if=0;
}
}
}