Bác namqn cho em hỏi tí, em vừa mới làm quen với dsPic em đang làm với con 30F3012, em sử dụng bộ kit dsPICDEM™ 2 DEVELOPMENT BOARD, thì có sử dụng được chương trình của bác không vậy.
Cảm ơn bác
Cảm ơn bác
#FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) #FUSES PR //Promiary Oscillator #FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled #FUSES WPSB16 //Watch Dog Timer PreScalar B 1:16 #FUSES WPSA512 //Watch Dog Timer PreScalar A 1:512 #FUSES PUT64 //Power On Reset Timer value 64ms #FUSES NOBROWNOUT //No brownout reset #FUSES BORV47 //Brownout reset at 4.7V #FUSES LPOL_HIGH //Low-Side Transistors Polarity is Active-High (PWM 0,2,4 and 6) //PWM module low side output pins have active high output polar #FUSES HPOL_HIGH //High-Side Transistors Polarity is Active-High (PWM 1,3,5 and 7) //PWM module high side output pins have active high output polarity #FUSES NOPWMPIN //PWM outputs drive active state upon Reset #FUSES MCLR //Master Clear pin enabled #FUSES NOPROTECT //Code not protected from reading #FUSES NOWRT //Program memory not write protected #FUSES NODEBUG //No Debug mode for ICD #FUSES NOCOE //Device will reset into operational mode #FUSES ICS0 //ICD communication channel 0 #FUSES RESERVED //Used to set the reserved FUSE bits
#FUSES NOWDT //No Watch Dog Timer #FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD) #FUSES PR //Promiary Oscillator #FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled #FUSES WPSB16 //Watch Dog Timer PreScalar B 1:16 #FUSES WPSA512 //Watch Dog Timer PreScalar A 1:512 #FUSES PUT64 //Power On Reset Timer value 64ms #FUSES NOBROWNOUT //No brownout reset #FUSES BORV47 //Brownout reset at 4.7V #FUSES LPOL_HIGH //Low-Side Transistors Polarity is Active-High (PWM 0,2,4 and 6) //PWM module low side output pins have active high output polar #FUSES HPOL_HIGH //High-Side Transistors Polarity is Active-High (PWM 1,3,5 and 7) //PWM module high side output pins have active high output polarity #FUSES NOPWMPIN //PWM outputs drive active state upon Reset #FUSES MCLR //Master Clear pin enabled #FUSES NOPROTECT //Code not protected from reading #FUSES NOWRT //Program memory not write protected #FUSES NODEBUG //No Debug mode for ICD #FUSES NOCOE //Device will reset into operational mode #FUSES ICS0 //ICD communication channel 0 #FUSES RESERVED //Used to set the reserved FUSE bits
#include "p30f4011.h"
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //Clock = 32 MHz
_FWDT(WDT_OFF);
_FBORPOR(MCLR_EN & PBOR_OFF & PWMxL_ACT_HI & PWMxH_ACT_HI);
_FGS(CODE_PROT_OFF);
//------------------------------------------------------------------------------
//Cac hang so cua chuong trinh (gia tri tuc thoi dung trong chuong trinh)
#define ms_count 125 //So dem tuong ung voi 1 ms, cho timer 1
#define LCD_EN _LATC13 //Tin hieu Enable cho LCD, chan RE8
#define LCD_RW _LATB8 //Tin hieu Read/Write cho LCD, chan RD0
#define LCD_RS _LATB7 //Tin hieu Reset cho LCD, chan RD1
#define LCD_D4 _LATC14 //Cac tin hieu Data cho LCD, chan (D4..D7)
#define LCD_D5 _LATE8
#define LCD_D6 _LATD1
#define LCD_D7 _LATD3
#define TRIS_LCD_D4 _TRISC14 //Tristate cua cac tin hieu Data cho LCD
#define TRIS_LCD_D5 _TRISE8
#define TRIS_LCD_D6 _TRISD1
#define TRIS_LCD_D7 _TRISD3
#define LCD_clear 0x01 //Xoa man hinh LCD
#define LCD_home 0x02 //Tro ve dau dong
#define LCD_normal 0x06 //Che do nhap du lieu binh thuong
#define LCD_normal_s 0x07 //Che do nhap du lieu binh thuong, dich man hinh
#define LCD_off 0x08 //Tat man hinh
#define LCD_on 0x0C //Bat man hinh
#define LCD_on_crsr 0x0E //Bat man hinh va con tro
#define LCD_on_blink 0x0F //Bat man hinh va con tro (nhap nhay)
#define LCD_4b2l 0x28 //Che do 2 dong, giao tiep 4 bit du lieu
#define LCD_homeL1 0x80 //Ve dau dong 1
#define LCD_homeL2 0xC0 //Ve dau dong 2
//Cac prototype cho cac chuong trinh con
void Init_PORTS(void); //Khoi tao cac cong I/O
void Init_TMR1(void); //Khoi tao Timer 1
void Delay_ms(unsigned int); //Tao tre mot so ms
void Init_LCD(void); //Khoi tao LCD
void LCD_cmd4(unsigned char); //Xuat lenh ra LCD qua giao tiep 4-bit
void LCD_dat4(unsigned char); //Xuat du lieu ra LCD qua giao tiep 4-bit
//Bang hang so trong ROM
char chuoi1[]="Hello world!"; //Chuoi ky tu se duoc xuat ra LCD
//Cac bien toan cuc
unsigned char time_out,idx;
//------------------------------------------------------------------------------
//Chuong trinh chinh
int main(void) {
_TRISE3 = 0;
Init_TMR1(); //Khoi tao cho TMR1
Init_PORTS(); //Khoi tao cac cong I/O
idx = 0;
Init_LCD();
Delay_ms(500);
LCD_dat4('E');
while (1) {
_LATE3=1;
Delay_ms(250); //Vong lap chinh
_LATE3=0;
Delay_ms(250);
};
}
//Chuong trinh con khoi tao Timer 1
//TMR1 duoc dung de dem so ms troi qua, o muc xung 8 Mips (fcy = 8 MHz)
//------------------------------------------------------------------------------
void Init_TMR1(void) {
TMR1 = 0; //Xoa so dem trong TMR1
PR1 = ms_count; //TMR1 tran moi ms
_T1IF = 0; //Xoa co ngat cua Timer 1
T1CON = 0x0020; //Dung fcy lam clock, prescale = 1:64, tat Timer 1
_T1IE = 1; //Cho phep ngat Timer 1
}
//Chuong trinh con khoi tao cac cong I/O, de noi voi LED
//------------------------------------------------------------------------------
void Init_PORTS(void)
{
LCD_D4 = 0; //Xoa cac bit chot du lieu LCD
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
TRIS_LCD_D4 = 0; //Cac chan du lieu LCD la ngo ra
TRIS_LCD_D5 = 0;
TRIS_LCD_D6 = 0;
TRIS_LCD_D7 = 0;
LCD_EN = 0;
_TRISC13 = 0; //RE8 la ngo ra, noi vao Enable cua LCD
LCD_RW = 0;
_TRISB8 = 0; //RD0 la ngo ra, noi vao R/W cua LCD
LCD_RS = 0;
_TRISB7 = 0; //RD1 la ngo ra, noi vao Reset cua LCD
}
//Chuong trinh con delay bang timer 1, nhan 1 tham so la so ms can delay
//------------------------------------------------------------------------------
void Delay_ms(unsigned n) {
PR1 = n*ms_count; //Dat nguong tran vao PR1
time_out = 0; //Xoa co het thoi gian cho
T1CONbits.TON = 1; //Bat timer 1
while (time_out == 0); //Cho den khi het thoi gian cho
T1CONbits.TON = 0; //Tat timer 1
TMR1 = 0;
}
//Chuong trinh con khoi tao module LCD
//------------------------------------------------------------------------------
void Init_LCD(void) {
LCD_cmd4(LCD_4b2l); //Dat che do giao tiep 4-bit, man hinh 2 dong
Delay_ms(1);
LCD_cmd4(LCD_off); //Tat man hinh
Delay_ms(1);
LCD_cmd4(LCD_normal); //Che do nhap du lieu binh thuong
Delay_ms(1);
LCD_cmd4(LCD_on_blink); //Bat man hinh, hien con tro nhap nhay
Delay_ms(1);
LCD_cmd4(LCD_clear); //Xoa man hinh
Delay_ms(5);
}
//Chuong trinh con xuat lenh o che do 4 bit
//------------------------------------------------------------------------------
void LCD_cmd4(unsigned char cmd) {
unsigned temp1, i;
LCD_RW = 0;
LCD_RS = 0; //Dat che do xuat lenh
temp1 = cmd >> 4; //Lay trang thai hien thoi cua LCD_DAT
LCD_D4 = temp1 & 1; //Xuat 4 bit cao
LCD_D5 = (temp1 & 2) >>1;
LCD_D6 = (temp1 & 4) >>2;
LCD_D7 = (temp1 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
temp1 = cmd & 0x0F; //Lay trang thai hien thoi cua LCD_DAT
LCD_D4 = temp1 & 1; //Xuat 4 bit thap
LCD_D5 = (temp1 & 2) >>1;
LCD_D6 = (temp1 & 4) >>2;
LCD_D7 = (temp1 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
}
//Chuong trinh con xuat du lieu o che do 4 bit
//------------------------------------------------------------------------------
void LCD_dat4(unsigned char dat) {
unsigned temp2, i;
LCD_RW = 0;
LCD_RS = 1; //Dat che do xuat du lieu
temp2 = dat >> 4; //Lay trang thai hien thoi cua LCD_DAT
LCD_D4 = temp2 & 1; //Xuat 4 bit cao
LCD_D5 = (temp2 & 2) >>1;
LCD_D6 = (temp2 & 4) >>2;
LCD_D7 = (temp2 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
temp2 = dat & 0x0F; //Lay trang thai hien thoi cua LCD_DAT
LCD_D4 = temp2 & 1; //Xuat 4 bit thap
LCD_D5 = (temp2 & 2) >>1;
LCD_D6 = (temp2 & 4) >>2;
LCD_D7 = (temp2 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
}
//Chuong trinh xu ly ngat Timer 1
//------------------------------------------------------------------------------
void _ISR _T1Interrupt(void) {
_T1IF = 0; //Xoa co ngat
time_out = 1;
}
void LCD_PutChar( char *buffer)
{
while(*buffer != '\0')
{
LCD_dat4(*buffer); /* calling another function */
/* to write each char to the lcd module */
buffer++;
}
} /* end of function */
void LCD_PutChar( char *buffer)
{
while(*buffer != '\0')
{
LCD_dat4(*buffer); /* calling another function */
/* to write each char to the lcd module */
buffer++;
}
} /* end of function */
void LCD_PutStr(char *buffer)
{
unsigned int i = 0;
while (buffer[i] != '\0')
{
LCD_dat4(buffer[i]); /* calling another function */
/* to write each char to the lcd module */
i++;
//Nên delay một khoảng thời gian giữa các ký tự
//để LCD có thời gian đáp ứng với yêu cầu trước đó
}
} /* end of function */
; Nhung tap tin can thiet khac: p30F4011.gld, p30f4011.h *
; Phan cung: *
; LCD_EN = RC13 *
; LCD_RW = RB8 *
; LCD_RS = RB7 *
; LCD_D4 = RC14 *
; LCD_D5 = RE8 *
; LCD_D6 = RD1 *
; LCD_D7 = RD3 *
; Chuong trinh nay dung che do giao tiep 4-bit voi LCD, *
------------------------------------------------------------------------------*/
#include "p30f4011.h"
_FOSC(CSW_FSCM_OFF & FRC_PLL4); //Clock = 32 MHz
_FWDT(WDT_OFF);
_FBORPOR(MCLR_EN & PBOR_OFF & PWMxL_ACT_HI & PWMxH_ACT_HI);
_FGS(CODE_PROT_OFF);
//------------------------------------------------------------------------------
//Cac hang so cua chuong trinh (gia tri tuc thoi dung trong chuong trinh)
#define ms_count 125 //So dem tuong ung voi 1 ms, cho timer 1
#define LCD_EN _LATC13 //Tin hieu Enable cho LCD, chan RE8
#define LCD_RW _LATB8 //Tin hieu Read/Write cho LCD, chan RD0
#define LCD_RS _LATB7 //Tin hieu Reset cho LCD, chan RD1
#define LCD_D4 _LATC14 //Cac tin hieu Data cho LCD, chan (D4..D7)
#define LCD_D5 _LATE8
#define LCD_D6 _LATD1
#define LCD_D7 _LATD3
#define TRIS_LCD_D4 _TRISC14 //Tristate cua cac tin hieu Data cho LCD
#define TRIS_LCD_D5 _TRISE8
#define TRIS_LCD_D6 _TRISD1
#define TRIS_LCD_D7 _TRISD3
#define LCD_clear 0x01 //Xoa man hinh LCD
#define LCD_home 0x02 //Tro ve dau dong
#define LCD_normal 0x06 //Che do nhap du lieu binh thuong
#define LCD_normal_s 0x07 //Che do nhap du lieu binh thuong, dich man hinh
#define LCD_off 0x08 //Tat man hinh
#define LCD_on 0x0C //Bat man hinh
#define LCD_on_crsr 0x0E //Bat man hinh va con tro
#define LCD_on_blink 0x0F //Bat man hinh va con tro (nhap nhay)
#define LCD_4b2l 0x28 //Che do 2 dong, giao tiep 4 bit du lieu
#define LCD_homeL1 0x80 //Ve dau dong 1
#define LCD_homeL2 0xC0 //Ve dau dong 2
#define lcd_line_two 0x40
//Cac prototype cho cac chuong trinh con
void Init_PORTS(void); //Khoi tao cac cong I/O
void Delay_ms(unsigned int); //Tao tre n ms
void Init_LCD(void); //Khoi tao LCD
void LCD_cmd4(unsigned char); //Xuat lenh ra LCD qua giao tiep 4-bit
void LCD_dat4(unsigned char); //Xuat du lieu ra LCD qua giao tiep 4-bit
void lcd_putstr(char *buffer); //Xuat 1 ky tu hay 1 chuoi ky tu
void lcd_gotoxy(int x, int y ); //Nhay den cot x, hang y
//------------------------------------------------------------------------------
//Chuong trinh chinh
void main()
{
_TRISE3 = 0;
Init_PORTS(); //Khoi tao cac cong I/O
Init_LCD(); //Khoi tao LCD
lcd_putstr("NGUYEN THE TUNG");
lcd_gotoxy(4,2);
lcd_putstr("DTD45-DH1");
while (1)
{
_LATE3=1;
Delay_ms(250); //Vong lap chinh
_LATE3=0;
Delay_ms(250);
}
}
//Chuong trinh con khoi tao cac cong I/O, de noi voi LED
//------------------------------------------------------------------------------
void Init_PORTS(void)
{
LCD_D4 = 0; //Xoa cac bit chot du lieu LCD
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
TRIS_LCD_D4 = 0; //Cac chan du lieu LCD la ngo ra
TRIS_LCD_D5 = 0;
TRIS_LCD_D6 = 0;
TRIS_LCD_D7 = 0;
LCD_EN = 0;
_TRISC13 = 0; //RE8 la ngo ra, noi vao Enable cua LCD
LCD_RW = 0;
_TRISB8 = 0; //RD0 la ngo ra, noi vao R/W cua LCD
LCD_RS = 0;
_TRISB7 = 0; //RD1 la ngo ra, noi vao Reset cua LCD
}
//Chuong trinh con delay bang timer 1, tre n ms
//TMR1 duoc dung de dem so ms troi qua, o muc xung 8 Mips (fcy = 8 MHz)
//------------------------------------------------------------------------------
void Delay_ms(unsigned int n)
{
TMR1 = 0; //Xoa so dem trong TMR1
T1CON = 0x0020; //Dung fcy lam clock, prescale = 1:64, tat Timer 1
PR1 = n*ms_count; //Dat nguong tran vao PR1
T1CONbits.TON = 1; //Bat timer 1
while (!_T1IF ); //Cho den khi het thoi gian cho
_T1IF = 0; //Xoa co ngat cua Timer 1
T1CONbits.TON = 0; //Tat timer 1
}
//Chuong trinh con khoi tao module LCD
//------------------------------------------------------------------------------
void Init_LCD(void) {
LCD_cmd4(LCD_4b2l); //Dat che do giao tiep 4-bit, man hinh 2 dong
Delay_ms(1);
LCD_cmd4(LCD_off); //Tat man hinh
Delay_ms(1);
LCD_cmd4(LCD_normal); //Che do nhap du lieu binh thuong
Delay_ms(1);
LCD_cmd4(LCD_on); //Bat man hinh, hien con tro nhap nhay
Delay_ms(1);
LCD_cmd4(LCD_clear); //Xoa man hinh
Delay_ms(5);
}
//Chuong trinh con xuat lenh o che do 4 bit
//------------------------------------------------------------------------------
void LCD_cmd4(unsigned char cmd) {
unsigned temp1, i;
LCD_RW = 0;
LCD_RS = 0; //Dat che do xuat lenh
temp1 = cmd >> 4; //Lay 4 bit cao
LCD_D4 = temp1 & 1; //Xuat 4 bit cao
LCD_D5 = (temp1 & 2) >>1;
LCD_D6 = (temp1 & 4) >>2;
LCD_D7 = (temp1 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
temp1 = cmd & 0x0F; //Lay 4 bit thap
LCD_D4 = temp1 & 1; //Xuat 4 bit thap
LCD_D5 = (temp1 & 2) >>1;
LCD_D6 = (temp1 & 4) >>2;
LCD_D7 = (temp1 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
}
//Chuong trinh con xuat du lieu o che do 4 bit
//------------------------------------------------------------------------------
void LCD_dat4(unsigned char dat) {
unsigned temp2, i;
LCD_RW = 0;
LCD_RS = 1; //Dat che do xuat du lieu
temp2 = dat >> 4; //Lay 4 bit cao
LCD_D4 = temp2 & 1; //Xuat 4 bit cao
LCD_D5 = (temp2 & 2) >>1;
LCD_D6 = (temp2 & 4) >>2;
LCD_D7 = (temp2 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
temp2 = dat & 0x0F; //Lay 4 bit thap
LCD_D4 = temp2 & 1; //Xuat 4 bit thap
LCD_D5 = (temp2 & 2) >>1;
LCD_D6 = (temp2 & 4) >>2;
LCD_D7 = (temp2 & 8) >>3;
LCD_EN = 1; //Tao xung Enable
for (i = 0; i < 2; i++); //keo dai toi thieu 1 us
LCD_EN = 0;
}
//Chuong trinh con xuat 1 ky tu hay 1 chuoi ky tu
//------------------------------------------------------------------------------
void lcd_putstr(char *buffer)
{
while(*buffer != '\0')
{
LCD_dat4(*buffer); /* calling another function */
/* to write each char to the lcd module */
buffer++;
Delay_ms(1);
}
}
//Chuong trinh con nhay den cot x, dong y
//------------------------------------------------------------------------------
void lcd_gotoxy(int x, int y)
{
int address;
if(y != 1) address = lcd_line_two;
else address=0;
address += x-1;
LCD_cmd4( 0x80 | address);
Delay_ms(1);
}
temp1 = LCD_LAT & 0xFFC3; //trang thai hien thoi cua LCD_LAT temp2 = (cmd >> 4) << 2; LCD_LAT = temp1 | temp2; //Xuat 4 bit cao ra RE5 toi RE2
Comment