Nguyên văn bởi lep
Thân,
.
#include<pic.h>
__CONFIG(HS & PWRTEN & BOREN & LVPDIS & WDTDIS );
// bộ font cho led7
const unsigned char Font[]={ 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001,\
0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001};
void init(void)
{
//-------------------------init for PORTs
OPTION = 0b00000000; // dùng Prescaler với k= 1:2
TRISD = 0b00000000;
TRISE = 0b11111111;
PORTD = Font[0] ; // ban đầu cho hiển thị số 0
//-------------------------init for interrupt vầ timer0
GIE = 1; // cho phép ngắt toàn cục
T0IE = 1; // cho phép ngắt timer0
TMR0 = 6; // đặt giá trị đầu cho TMR0
}
void interrupt MyInt(void){
static int counter; // biến đếm sự kiện ngắt timer
static unsigned char i; // biến đếm số đang hiển thị trên led
if (T0IF & T0IE){ // nếu có ngắt
T0IF = 0 ;
TMR0 = 6;
++ counter;
if( counter == 1000){ // cứ 1s thì hiển thị hiển thị tiếp
counter = 0 ; // reset lại bộ đếm
PORTD = Font[i++];
if( i ==10) i = 0;
};
};
}
void main(){
init();
while(1);
}
if (T0IF & T0IE){
if (T0IF)
. Nếu không nhận ra thì có khi sai nhiều đó
. Khi chúng ta viết chương trình ngắt trong ngắt thì sẽ thấy điều này
.
. Bạn nào test luôn lại cái và báo lại kết quả cho mình luôn nha. 


#include<pic.h>
__CONFIG(HS & PWRTEN & BOREN & LVPDIS & WDTDIS );
const unsigned char Font[]={ 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001,\
0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001};
//===== hàm khoi tao
void interrupt MyInt(void){
static int counter1,counter2;
// counter1 dem so lan Timer0 tran
// counter2 dem so lan ca 4 led duoc quet
static unsigned int number;
static unsigned int a[4]={0,0,0,0};
static unsigned char j; // biến đếm kiểm tra xem led nào đang được quét
if(T0IF & T0IE){
TMR0 = 6;
T0IF = 0 ;// xoa co ngat
if( ++counter1 == 10){
counter1 = 0;
PORTD = 0xFF;
PORTA = 0xFF - (1<< (j+1)) ;
PORTD = Font[a[j]];
if(j++ == 3 ) {
j=0;
RD0 = 0;
++counter2;
};
};
if(counter2 == 30 ){ // cap nhat so vao sau 300 ms
counter2 = 0;
if (ADIF == 0) {
ADGO = 1 ; // chuyen doi ADC tiep
a[3] = (unsigned char)number/1000;
a[2] = (number/100)%10;
a[1] = (number/10)%10;
a[0] = number%10;
};
};
};
if ( ADIE & ADIF){
//number = (unsigned int)((((ADRESH <<8) + ADRESL)*500)>>10);
//number = (unsigned int)((ADRESH * 256 + ADRESL ) * 500)>>10;
number=(ADRESH<<8)|ADRESL;
number=(unsigned int) (number* 4.8876); //5000/1023
ADIF = 0 ;
};
}
//Hàm khoi tao
void init(void)
{
//Khoi tao cho cac port
OPTION = 0b00000000;
TRISA = 0b00000001; // RA0 là input cho dau vao cua analog
TRISB = 0b11111111;
TRISC = 0b11111111;
TRISD = 0b00000000;
PORTD = Font[0];
TRISE = 0b11111111;
//Khoi tao cho ADC, co the viet don gian theo thanh ghi cung ok, viet nhu sau cho chinh sua don gian
ADFM = 1; // chon can le ben phai voi ket qua 10 bit
PCFG3 = PCFG2 = PCFG1 = 1; PCFG0 = 0;
// chon 1 dau RA0 la Analog con l?i la Digital, chon ap tham chieu la VDD - VSS
ADCS2 = ADCS1 = ADCS0 = 0 ; // chon tan so chuyen doi AD
CHS2 = CHS1 = CHS0 = 0 ; // chon chanle AN0 lam chuyen doi ADC
ADGO = 0 ;
ADON = 1; // Bat AD module
// Khoi tao cho ngat
GIE = 1; // Global Interrupt Enable
T0IE = 1; // Ngat timer0
TMR0 = 6;
PEIE = 1; // enable PI
ADIE = 1 ; // Ngat AD Module
}
//------------------------------------MAIN
void main(){
init();
ADGO = 1 ; // Bat dau chuyen doi ADC
while(1);
}
//=============== Hết
PWM Period = [ PR2 +1 ] x 4 x Tosc x (TMR2 Prescaler Value) PWM Duty Cycle = [ CCPR1L : CCP1CON<5:4>) x Tosc x (TMR2 Prescaler Value) => CCPR1L : CCP1CON<5:4> = k% x (TMR2 + 1 ) x 4; với k là độ rộng xung (%) k = 0 -> 100
/*;========================================================
; Ten chuong trinh : Dieu che do rong xung
; Nguoi thuc hien : Ngo Hai Bac (NOHB)
; Ngay thuc hien : 12/04/06
; Phien ban : 1.0
; Mo ta phan cung : Dung PIC16F877A - thach anh 20MHz
; Dieu che do rong xung ra chan RC2/CCP1, chan RC2 noi voi led roi noi voi tro 220 Ohm, roi noi voi dat.
; => sau 1s thi tang do rong xung len 10%
;----------------------------------------------------------------
; Ngay hoan thanh :
; Ngay kiem tra :
; Nguoi kiem tra :
;----------------------------------------------------------------
; Chu thich : Phan chuong trinh nho cho du an lon :D
PWM dung timer2
;========================================================*/
#include<pic.h>
__CONFIG(HS & UNPROTECT & PWRTEN & BOREN & LVPDIS & WDTDIS );
//============ cac marco
//============= ham khoi tao
void init(void){
//===== init for PWM
// PWM Period = [ PR2 +1 ] x 4 x Tosc x (TMR2 Prescaler Value)
PR2 = 82; // Tan so = 15 KHz, TMR2 Prescaler Value = 4
// PWM Duty Cycle = [ CCPR1L : CCP1CON<5:4>) x Tosc x (TMR2 Prescaler Value)
// => CCPR1L : CCP1CON<5:4> = k% x (TMR2 + 1 ) x 4;
CCPR1L = 0;
TRISC2 = 0; // RC2 la output
RC2 = 1;
T2CON = 0b00000101; // Timer2 on, TMR2 Prescaler Value = 4
CCP1CON = 0b00001100; // CCP1 hoat dong trong che do PWM
//===== init cho SFR
OPTION = 0x00; // dung tro keo len PortB, TMR0 Prescaler = 2 va dung cho TMR0
TMR0 = 0x06; // khoi tao cho Timer0
T0IF = 0; // xoa co ngat timer0
T0IE = 1; // timer0 Interrupt Enable
GIE = 1; // Global Interrupt Enable
}
//============= ham ngat
void interrupt MyInt(void){
static volatile unsigned char k; // % do rong xung => 0<= k <= 100
static volatile unsigned int counter1;
int temp;
// ==== Ngat timer0
if (T0IE & T0IF ){
T0IF = 0;
TMR0 = 6;
if(++counter1 == 1000){
counter1 = 0;
temp = 4* (PR2 + 1) * k/100;
CCPR1L = temp>>2;
CCP1Y = temp - ((temp>>1)<<1);//temp%2;
CCP1X = (temp>>1)%2;
//CCPR1L = (TMR2 + 1)>>1 ;
k += 10; // tang 10%
if( k > 100) k=0;
};
};
}
//============= ham chinh Main
void main(void){
init();
while(1);
}
.
/*========================================================
; Ten chuong trinh : giao tiep USART voi PC, nhan duoc thi hien thi lai
; Nguoi thuc hien : ngohaibac
; Ngay thuc hien :
; Phien ban : 1.0
; Mo ta phan cung : Dung PIC16F877A - thach anh 20MHz
; chan RC7/TX noi voi chan R1OUT cua MAX232
; chan RC6/RX noi voi chan T1IN cua MAX232
;----------------------------------------------------------------
; Ngay hoan thanh :
; Ngay kiem tra :
; Nguoi kiem tra :
;----------------------------------------------------------------
; Chu thich : Chuong trinh dung giao tiep USART
;========================================================*/
#include<pic.h>
# include "stdio.h"
__CONFIG(HS & PWRTEN & BOREN & LVPDIS & WDTDIS );
// ham init - khoi tao
void init(void){
// Khoi tao cho USART
TRISC = TRISC | 0b11000000; // Bit 7,6 la RX va TX phai la Input
SPEN = 1 ; // Enable Serial Port communication.
SYNC = 0; // che do khong dong bo Asynchronous
// cho che do truyen
TXEN = 1; //enable truyen
TX9 = 0 ; // chon che do 8 truyen bit du lieu
BRGH = 1; // High - speed
SPBRG = 129; //Baud = 9600; voi HS, Baud Rate = Fosc/(16(X+1))
TXIE = 0; // khong dung ngat Truyen
// Che do nhan du lieu
RCIE = 1; // dung ngat nhan
PEIE = 1; // cho phep ngat ngoai vi
GIE = 1; // cho phep ngat toan cuc
RX9 = 0; // nhan 8 bit
CREN = 1; // lien tuc nhan du lieu; = 0 k lien tuc nhan du lieu
}
// ham truyen du lieu
void putch(unsigned char byte)
{
/* output one byte */
while(!TXIF); /* set when register is empty */
// continue;
TXREG = byte;
}
// ham ngat
void interrupt MyInt(void){
unsigned char temp;
if(RCIF){ // Neu co ngat nhan du lieu noi tiep
temp = RCREG;
RCIF = 0; // Xoa co ngat RCIF
printf(" da nhan duoc du lieu: %c", temp);
};
}
// ham chinh
void main(void){
init();
while(1);
}
.
hold off; x_rough= 0:0.01:5; %x_fine= 0:0.05:0.8 ;% chon buoc noi suy tu xmin:0.1:xmax( voi xmin la gia tri nho nhat cua x_rough, xmax tuong tu; x_fine = 0:0.001: 5; data = [ ]; // Các giá trị số xung đo được 500 giá trị data = data * 1000 /(10 * 100 *12); % tính toán ra tốc độ thực và chia cho 12V để suy ra đáp ứng khi U = 1V vì coi động cơ DC là tuyến tính % 100 là số lỗ /1 vòng của Encoder inter_cubic= interp1(x_rough,data,x_fine,'cubic'); % noi suy theo phuong phap cubic plot(x_fine,inter_cubic,'k','LineWidth',2); grid on;
#include<pic.h>
# include "stdio.h"
__CONFIG(HS & PWRTEN & BOREN & LVPDIS & WDTDIS );
const unsigned char Font[]={ 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001,\
0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001};
# define Ts 10 // Thoi gian trich mau Ts (ms)
# define Encoder_resolution 100 // So lo cua Encoder/1 vong
# define Total_Point 500 // So diem can trich mau
// ham truyen du lieu
void putch(unsigned char byte)
{
/* output one byte */
while(!TXIF); /* set when register is empty */
// continue;
TXREG = byte;
}
//===== Ham khoi tao
void interrupt MyInt(void){
static volatile unsigned int i, Pulse_Encoder;
// i la so diem trich mau
// Pulse_Encoder = so xung do duoc tu Encoder
static volatile unsigned char allow,counter_Sampling;
// allow la bien cho phep bat dau trich mau
// counter_Sampling: bien dem de trich mau
unsigned char RC_USART; // gia tri doc duoc tu thanh ghi nhan du lieu USART
if(T0IF & T0IE){
TMR0 = 6;
T0IF = 0 ;// xoa co ngat
if((++counter_Sampling == Ts) && allow){ // Ts = 10 ms
Pulse_Encoder = (TMR1H << 8) | TMR1L;
// Toc do dong co = Pulse_Encoder * 1000 / (Encoder_resolution * Ts) (vong/s)
printf("%6d",Pulse_Encoder); // Truyen gia tri xung do tren Ts
counter_Sampling = 0;
if (i++ == Total_Point){
i = 0; // Neu trich duoc 500 diem thi dung dong co lai
CCPR1L = 0;
allow = 0;
};
};
};
if(RCIF){ // Neu co ngat nhan du lieu noi tiep
RC_USART = RCREG;
RCIF = 0; // Xoa co ngat RCIF
switch(RC_USART){
case 'P': // === Bat dau cho dong co chay cho do rong xung = 100%
TMR1ON = 0 ; // ngung Timer1
TMR1H = TMR1L = 0;
TMR1ON = 1 ; // bat lai Timer1
CCPR1L = PR2 + 1;
CCP1Y = CCP1X = 0;
allow = 1;
break;
};
};
}
//------------------------------------init
void init(void){
// Khoi tao cho USART
TRISC = TRISC | 0b11000000; // Bit 7,6 la RX va TX phai la Input
SPEN = 1 ; // Enable Serial Port communication.
SYNC = 0; // che do khong dong bo Asynchronous
// cho che do truyen
TXEN = 1; //enable truyen
TX9 = 0 ; // chon che do 8 truyen bit du lieu
BRGH = 1; // High - speed
SPBRG = 129; //Baud = 9600; voi HS, Baud Rate = Fosc/(16(X+1))
TXIE = 0; // khong dung ngat Truyen
// Che do nhan du lieu
RCIE = 1; // dung ngat nhan
PEIE = 1; // cho phep ngat ngoai vi
GIE = 1; // cho phep ngat toan cuc
RX9 = 0; // nhan 8 bit
CREN = 1; // lien tuc nhan du lieu; = 0 k lien tuc nhan du lieu
//===== init for PWM
// PWM Period = [ PR2 +1 ] x 4 x Tosc x (TMR2 Prescaler Value)
PR2 = 82; // Tan so = 15 KHz, TMR2 Prescaler Value = 4
// PWM Duty Cycle = [ CCPR1L : CCP1CON<5:4>) x Tosc x (TMR2 Prescaler Value)
// => CCPR1L : CCP1CON<5:4> = k% x (TMR2 + 1 ) x 4;
CCPR1L = 0;
TRISC2 = 0; // RC2 la output
RC2 = 1;
T2CON = 0b00000101; // Timer2 on, TMR2 Prescaler Value = 4
CCP1CON = 0b00001100; // CCP1 hoat dong trong che do PWM
//================= init for PORTs
OPTION = 0b00000000;
ADCON1 = 0b00000111;
TRISA = 0b00000000;
TRISD = 0b00000000;
PORTD = 0xFF;
//================ Khoi tao cho Timer0
GIE = 1;
T0IE = 1;
TMR0 = 6;
// ========= Khoi tao cho dem Timer1
TMR1CS = 1; // Timer1 hoat dong o che do Counter Mode
T1SYNC = 1; // TMR1 hoat dong o che do dem khong dong bo
T1OSCEN = 1; // Dem xung tu chan RC1/T1OSI/CCP2
T1CKPS1 = T1CKPS0 = 0; // Chon Prescaler = 1:1
TMR1H = TMR1L = 0;
TMR1IE = 0 ; // Khong dung ngat Timer1
TMR1ON = 1; // Bat Timer1
}
//------------------------------------MAIN
void main(){
init();
while(1);
}
// === Kết thúc
.
//========================================================
//Bai toan phanh xe.Phanh duoc noi vao RB0
//khi phanh thi RB0 o muc thap
//binh thuong RB0 o muc cao
//co 8 led o RD8 den RD0 se sang theo quy luat(khi phanh)
//10000000,01000000...00000001,11111111,00000000
//sau do lap lai neu chua nha phanh
//thoi gian delay la 1 giay
//thach anh 20 Mhz
//========================================================
#include<pic.h>
#define out PORTD
__CONFIG(HS & PWRTEN & BOREN & LVPDIS &WDTDIS );
void DL(unsigned int giay);
//--------------------------------------------------------------------------------------------------
const unsigned char a[]={0b10000000,0b01000000,0b00100000,0b00010000,0b00001000,0b00000100,0b00000010,0b00000001,0b11111111,0b00000000};
//========================================================
void main()
{
unsigned char i;
TRISB = 0b11111111; //Dinh nghia cong B la INPUT
RBPU =0; //co dien tro trong keo len nguon
TRISD = 0b00000000;
i=0;
while(1)
{
if(RB0==0) //nhan phanh
{
out=a[i];
i=i+1;
DL(25000); //delay 1giay
if(i==11) i=0;
}
else
{
out=0;
i=0;
}
}
}
//------------------------------------------------------------------------
void DL(unsigned int giay)
{
while(giay--)
{
//delay 40 microgiay
PSA=1; //bo chia dung cho wdt
T0CS=0; //dung dem thoi gian
TMR0=56;
while(!TMR0IF);
TMR0IF=0;
}
}
//=============================THE END===========================
.
._Portvar EQU 06h
[B]volatile unsigned[/B] char Portvar [B]@ [/B]0x06;
// LCD.h #ifndef LCD_H #define LCD_H # include <pic.h> //============== Using LCD - 2 line, 16 colum #define LCD_MODE_2x16 (TRUE) // HDM16216H-2, HDM16216H-4, HDM16216H-5, HDM16216H-B, HDM16216H-D, HDM16216L-2, HDM16216L-5, HDM16216L-7, HDM16216L-D, HDM16216L-B, HDM16216H-I #define LCD_CURSOR_BLINK (FALSE) // Blink/Noblink cursor mode #define LCD_CURSOR_ON (TRUE) // Cursor visible #define LCD_CURSOR_INCREMENT (TRUE) // Set left-to-right cursor movement #define LCD_CURSOR_SHIFT (FALSE) // Shift display on entry #define LCD_DISPLAY_ON (TRUE) // Turn display on/off #define LCD_ALLOW_USER_CHARS (TRUE) // Controls whether display uses ASCII for control chars or uses user-defined chars in lcd_putc() #define LCD_ENABLE_GETC (TRUE) // Save code space by setting to FALSE #define LCD_ENABLE_GOTOXY (TRUE) // any functions which you will not #define LCD_ENABLE_PRINTF (TRUE) // need in your application. #define LCD_ENABLE_UNSCROLL (TRUE) #define LCD_ENABLE_SCROLL (TRUE) #define LCD_ENABLE_CLEAR (TRUE) #define LCD_MAXROWS ((unsigned char)(2)) #define LCD_MAXCOLS ((unsigned char)(16)) # define LCD_MULTI_LINE (TRUE) #define LCD_8_BIT_MODE (TRUE) //======================= Define LCD_PORT #define LCD_DATA_PORT PORTD // Port on which LCD data lines are connected #define LCD_TRIS_PORT TRISD // Need to specify the corresponding TRIS # define LCD_RS RA1 # define LCD_RW RA2 # define LCD_E RA3 # define LCD_TRIS_E TRISA1 # define LCD_TRIS_RW TRISA3 # define LCD_TRIS_RS TRISA2 // ======================= Define command code #define LCD_COMMAND_CLEAR ((unsigned char)(0x01)) //1.53 ms // Clear screen, home cursor, unshift display #define LCD_COMMAND_HOME ((unsigned char)(0x02)) //1.53 ms // Home cursor, unshift display #define LCD_COMMAND_BACKSPACE ((unsigned char)(0x10)) // Move cursor left one (destructive based on LCD_DESTRUCTIVE_BS) #define LCD_COMMAND_FWDSPACE ((unsigned char)(0x14)) // Move cursor right one #define LCD_COMMAND_PANLEFT ((unsigned char)(0x18)) // Move screen left one #define LCD_COMMAND_PANRIGHT ((unsigned char)(0x1C)) // Move screen right one // ==================== Declare some functions void [B]delay_us[/B] (unsigned char us); void [B]delay_ms[/B](unsigned char ms); unsigned char [B]LCD_getByte[/B] (void); void [B]LCD_PrByte [/B](unsigned char c); void [B]LCD_Command[/B](unsigned char c); void [B]LCD_PrChar[/B] (unsigned char c); void [B]LCD_gotoXY[/B](unsigned char row, unsigned char col); unsigned char [B]LCD_getChar[/B] (void); // Read character at cursor void [B]LCD_Init[/B](void); void [B]LCD_PrString[/B] (const char* message); #endif
//LCD.c
# include "lcd.h"
// ==== Some constants for LCD 16x2
const unsigned char const LCD_ROW_ADDRESS[] = // Row/Column information for LCD_gotoxy()
{
0x00, // Line 1
0x40 // Line 2
};
const unsigned char const LCD_INIT_STRING [] = // LCD Init String on powerup
{
0b00000001, // Clear display
0b00000010, // Home cursor
};
//==========================================================================================
void delay_us (unsigned char us){
while(us--){
asm("nop");
asm("nop");
};
}
//==========================================================================================
void delay_ms(unsigned char ms){
unsigned char i, j;
while(ms--){
for (i = 0; i < 20; i++)
for (j = 0; j < 100; j++)
asm("nop");
};
}
// ============================================================================================
unsigned char LCD_getByte (void){
unsigned char retval; // Return value
LCD_TRIS_PORT = 0xFF; // Set port to all input
LCD_RW = 1; // Tell LCD we want to read
delay_us(2);
LCD_E = 1; // Do the read
delay_us(2);
retval = LCD_DATA_PORT;
LCD_E = 0;
LCD_TRIS_PORT = 0x00; // Set port back to outputs
return (retval);
}
//====================================================================================================
void LCD_PrByte (unsigned char c){ // Write byte to port in current RS mode
unsigned char RS_Status;
RS_Status = LCD_RS; // Get old pin state
LCD_RS = 0; // Force into command mode to read state
while (LCD_getByte () & 0x80); // Wait for read state
if (RS_Status)
LCD_RS = 1; // Restore RS to old state
delay_us (1);
LCD_RW = 0; // Set to write mode
delay_us (1);
LCD_DATA_PORT = c; // Send the character out
LCD_E = 1;
delay_us(1);
LCD_E = 0;
}
//================================================================================================
/*Ham yeu cau goi lenh dieu khien LCD*/
void LCD_Command(unsigned char c){ // Send command to LCD port
LCD_RS = 0;
LCD_PrByte(c);
}
//================================================================================================
/*Ham yeu cau goi du lieu hien thi len LCD*/
void LCD_PrChar (unsigned char c){ // Write character to LCD
LCD_RS = 1 ;
LCD_PrByte(c);
}
//=================================================================================================
void LCD_gotoXY(unsigned char row, unsigned char col){
if (row > LCD_MAXROWS) // Range limit
row = LCD_MAXROWS;
if (col > LCD_MAXCOLS)
col = LCD_MAXCOLS;
row = LCD_ROW_ADDRESS[row-1]; // Get address of first byte on desired row
row += col - 1;
LCD_Command (0x80 | row); // Write new cursor address
}
// =====================================================================================
unsigned char LCD_getChar (void) // Read character at cursor
{
unsigned char retval; // Return value
LCD_RS = 1;
retval = LCD_getByte ();
LCD_RS = 0;
return (retval);
}
//=======================================================================================
void LCD_Init(void){
unsigned char i;
LCD_TRIS_PORT = 0x00;
LCD_TRIS_E = 0;
LCD_TRIS_RW = 0;
LCD_TRIS_RS = 0;
LCD_E = 0 ;
LCD_RS = 0;
LCD_RW = 0;
//delay_ms(100); // Tao tre 100ms cho LCD khoi dong
LCD_PrByte (0b00111000); //Function set ; DL = 1(8 bits) , N = 1 (2 lines), F = 0 (5x8 dots)
LCD_PrByte (0b00001100); // Display on/off control
LCD_PrByte (0b00000110); // Entry mode set
LCD_PrByte(LCD_COMMAND_CLEAR);
LCD_PrByte(LCD_COMMAND_HOME);
}
// ===================================================================================================
void LCD_PrString (const char* message){ // Write message to LCD (C string type)
while (*message) // Look for end of string
LCD_PrChar (*message++); // Show and bump
}
Comment