PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp USB, CAN, I2C, SPI, USART...

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Giao tiếp USB, CAN, I2C, SPI, USART... Những giao tiếp được tích hợp trên PIC

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 22-02-2011, 08:55 AM   #4
vinhuct
Nhập môn đệ tử
 
Tham gia ngày: Dec 2010
Bài gửi: 4
:
Em mới học Pic, không hiểu lắm về các timer. Mong các Anh chuyển giup đoạn code này sang chi PIC, Em đã chuyển được 1 phần, nhưng đoạn sau thì tit. Cảm ơn các Anh.
#include<reg2051.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
#include<absacc.h>
#include <math.h>
#include<intrins.h>
#include<stdlib.h>
//***Khai bao bien****
sbit Clock=P1^0;
sbit STcp =P1^1;
sbit Data =P1^2;

sbit SelGre1=P1^3;
sbit SelGre2=P1^4;
sbit SelRed1=P1^5;
sbit SelRed2=P1^6;


sbit R=P3^2;
sbit Y=P3^3;
sbit G=P3^4;
//*************
unsigned char Code_7S[10]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF 6};//={0xDE,0x50,0xEC,0xF8,0x72,0xBA,0xBE,0xD0,0xFE,0xF A};
unsigned char LED_BUF[2];

unsigned char tic=0,tic2=0;
Sig_in,Num_Err=0;

char G_inc=0,G_dec=0,
R_inc=0,R_dec=0;

bit Flag_1s;
//**************************************
//**************************************
void wait (unsigned int input){
while (input--){;}
}
void Disp_led(char input){
char i;
if(input<0)input=0;
if(input<100){
LED_BUF[0]=Code_7S[input/10];
LED_BUF[1]=Code_7S[input%10];
}
STcp=0;
Clock=0;
for(i=0;i<=7;i++){
if((LED_BUF[0]&0x80)==0)Data=0;
else Data=1;
Clock=1;
LED_BUF[0]=LED_BUF[0]<<1;
Clock=0;
}
for(i=0;i<=7;i++){
if((LED_BUF[1]&0x80)==0)Data=0;
else Data=1;
Clock=1;
LED_BUF[1]=LED_BUF[1]<<1;
Clock=0;
}
STcp=1;
}
void Init_time(void){
while(G);
TR0=1;
while(!G)if(Flag_1s){Flag_1s=0;G_inc++;}
TR0=0;TH0=0xD8;TL0=0xF0;tic=0;
if(G_inc>0){G_dec=G_inc;G_inc=0;}
while(R);
TR0=1;
while(!R)if(Flag_1s){Flag_1s=0;R_inc++;}
TR0=0;TH0=0xD8;TL0=0xF0;tic=0;
if(R_inc>0){R_dec=R_inc;R_inc=0;}
}

void main(void){
SelGre1=1;SelGre2=1;
SelRed1=1;SelRed2=1;
wait(5000);
Flag_1s=0;
//---Init timer0--
TMOD=0x11;//khoi tao cho Timer0 o che do timer 16 bits
TR0=0;TH0=0xD8;TL0=0xF0;tic=0;
ET0=1;//cho phep ngat Timer0
//---Init timer1--
EA=1;//Cho phep ngat toan cuc
Init_time();
//***********
while(1){
while(G);
TR0=1;Disp_led(G_dec);SelGre1=0;SelGre2=0;SelRed1= 1;SelRed2=1;
while(!G)if(Flag_1s){Flag_1s=0;G_inc++;G_dec--;Disp_led(G_dec);}
TR0=0;TH0=0xD8;TL0=0xF0;tic=0;
if(G_inc>0){G_dec=G_inc;G_inc=0;}
SelGre1=1;SelGre2=1;
SelRed1=1;SelRed2=1;
while(R);
TR0=1;Disp_led(R_dec);SelGre1=1;SelGre2=1;SelRed1= 0;SelRed2=0;
while(!R)if(Flag_1s){Flag_1s=0;R_inc++;R_dec--;Disp_led(R_dec);}
TR0=0;TH0=0xD8;TL0=0xF0;tic=0;
if(R_inc>0){R_dec=R_inc;R_inc=0;}
}
}
//----------------------------------------------------------------------------------------------------------
void INTTimer0_10ms(void) interrupt 1{
TR0=0;TH0=0xD8;TL0=0xF0;TR0=1;
tic++;
if(tic>=100){
Flag_1s=1;
tic=0;
}
}




Và đây là đoạn Em sửa:
#include <18F4431.h>
#include <math.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
//***Khai bao bien****
#define Clock PIN_B0
#define STcp PIN_B1
#define Data PIN_B2

#define SelGre1 PIN_B3
#define SelGre2 PIN_B4

#define SelRed1 PIN_B5
#define SelRed2 PIN_B6


#define R PIN_B7
#define Y PIN_D0
#define G PIN_D1
//*************
unsigned char Code_7S[10]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF 6};//={0xDE,0x50,0xEC,0xF8,0x72,0xBA,0xBE,0xD0,0xFE,0xF A};
unsigned char LED_BUF[2];

unsigned char tic=0,tic2=0,
Sig_in,Num_Err=0;

char G_inc=0,G_dec=0,
R_inc=0,R_dec=0,Flag_1s,TMOD,count;


//**************************************
//**************************************

void Disp_led(char input){
char i;
if(input<0)input=0;
if(input<100){
LED_BUF[0]=Code_7S[input/10];
LED_BUF[1]=Code_7S[input%10];
}
output_low(STcp);
output_low (Clock);
for(i=0;i<=7;i++){
if((LED_BUF[0]&0x80)==0)output_low(Data);
else output_high(Data);
output_high (Clock);
LED_BUF[0]=LED_BUF[0]<<1;
output_low (Clock);
}
for(i=0;i<=7;i++){
if((LED_BUF[1]&0x80)==0)output_low (Data);
else output_high (Data);
output_high (Clock);
LED_BUF[1]=LED_BUF[1]<<1;
output_low(Clock);
}
output_high (STcp);
}
....
Không viết được nữa, hiii..Các Ah giúp Em nhé.
vinhuct vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 09:22 PM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam