1

我为 LCD 与 tivaC 启动板接口编写了一个代码,但是当我尝试运行它时似乎没有发生任何事情工作。代码中是否有任何逻辑错误无法使 LCD 执行它。

#include "tm4c123gh6pm.h"
#define LCD_RS (*((volatile unsigned long *)0x40004200))    //PA.7 for 
  register select pin
#define LCD_EN  (*((volatile unsigned long *)0x40004100))   //PA.6 for enable 
 pin
#define LCD_RW  (*((volatile unsigned long *)0x40004080))   //PA.5 for rw pin

/*function protoypes */
void LCD_INIT(void);
void LCD_CMD(unsigned long cmd);
void LCD_WRITE (char data);
void Delay(void);

/*this function must be writen as keil 
allows running of a function before the main*/
void SystemInit() {
};

int main (void) {
    LCD_INIT();
    while(1) {
        LCD_CMD(0X38);  //8-bit bus mode, 2 line display mode, 5x8 dots display mode
        Delay();
        LCD_CMD(0X01);  //clear display
        Delay();
        LCD_CMD(0X10);  //cursor display shift
        Delay();
        LCD_CMD(0X0C);  //diplay is on
        Delay();
        LCD_WRITE('A'); //send character A on the LCD 
        Delay();
    }
}

/*this function will initate the LCD it initializes 
the portA5-6-7 as the control pins  for the LCD and portB0-7 
as the data pins*/
void LCD_INIT(void) {
    volatile unsigned long delay;
    SYSCTL_RCGC2_R |= 0X00000002;   // allow the clock for portB
    delay = SYSCTL_RCGC2_R;     // short delay for clock
    GPIO_PORTB_AFSEL_R &= ~0xff;    //disable alternative functions for portB
    GPIO_PORTB_AMSEL_R &= ~0Xff;    //disable analogue function
    GPIO_PORTB_PCTL_R &= ~0XFF;     //regular digital pins
    GPIO_PORTB_DIR_R  |= 0XFF;      //set the direction of PB0-7 as     output
    GPIO_PORTB_DEN_R  |= 0XFF;      //enable digital portB

    SYSCTL_RCGC2_R |= 0X00000001;   // allow the clock for PA5,6,7
    delay = SYSCTL_RCGC2_R;     // short delay for clock
    GPIO_PORTA_AFSEL_R &= ~0xE0;    //disable alternative functions for PA5,6,7
    GPIO_PORTA_AMSEL_R &= ~0XE0;    //disable analogue function for PA5,6,7
    GPIO_PORTA_PCTL_R &= ~0XE0;     //regular digital pins
    GPIO_PORTA_DIR_R |= 0XE0;       //set the direction of PA5,6,7 as output
    GPIO_PORTA_DEN_R |= 0XE0;       //enable digital PA5,6,7
}

//this function passes the command to the LCD
void LCD_CMD(unsigned long cmd) {
    GPIO_PORTB_DATA_R = cmd;    //set PB7-0 as the passed command to the function
    LCD_RS = 0x00;  //set PA7 register select pin to low
    LCD_RW = 0x00;  //set PA5 r/w pin to low
    LCD_EN = 0x40;  //set enable pin to high
    Delay();        //short delay 
    LCD_EN = 0x00;  //set enable pin to low 
}

//this function passes the data to the LCD
void LCD_WRITE (char data) {
    GPIO_PORTB_DATA_R = data;   //write the data to PB7-0
    LCD_RS = 0x80;  //set PA7 to high
    LCD_RW = 0x00;  //set pA5 to low
    LCD_EN = 0x40;  //set the enable pin high
    Delay();        //short delay
    LCD_EN = 0x00;  //set the enable pin to low
}

//short delay function
void Delay(void) {
    unsigned long  time;
    time = 12000; 
    while(time){
        time--;
    }
}
4

0 回答 0