pic单片机c语言触摸屏程序,0802LCD 4位显示程序 PIC单片机C语言程序

#include

#include

//#include"head.h"

#define uchar unsigned char

#define uint unsigned int

#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)

#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)

#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)

#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)

#pragma config LVP = ON        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)

#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)

#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)

#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

//char tab[]="0123456789";

#define uchar unsigned char

#define uint unsigned int

void LCD_write_cmd(uchar command);

void LCD_write_command(uchar command);

void LCD_en_write(void);

void LCD_set_xy( unsigned char x, unsigned char y );

void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);

void LCD_write_data(unsigned char data);

void delay_nus(unsigned int n);

void delay_nms(unsigned int n);

#define RS RC2

#define EN RC3

#define DB7 RC7

#define DB6 RC6

#define DB5 RC5

#define DB4 RC4

#define LCD_DATA_PORT PORTC

#define LCD_DATA_TRIS TRISC

/*------------------------------------------------------------------------------

函数说明

------------------------------------------------------------------------------*/

void LCD_init()

{

TRISD=0x00; //数据口方向为输出

PORTD=0x00;

TRISC=0x00; //数据口方向为输出

PORTC=0x00;//设置EN、RS/数据为输出

LCD_write_cmd(0x30);//4位的指令

delay_nms(5);

LCD_write_cmd(0x30);

delay_nus(200);

LCD_write_cmd(0x30);

delay_nms(1);

LCD_write_cmd(0x20);

LCD_write_cmd(0x20); //4位显示

LCD_write_cmd(0x80);

LCD_write_cmd(0x00); //显示开 游标、闪烁不显示

LCD_write_cmd(0x80);

LCD_write_cmd(0x00); //清屏

LCD_write_cmd(0x01);

LCD_write_cmd(0x00); //两行 5*7

LCD_write_cmd(0xc0);

}

void LCD_write_cmd(uchar command) //写指令

{

delay_nus(10);

RS=0;

EN=0;//使能清零

LCD_DATA_PORT&=0x0f; //清除端口

delay_nus(2);

EN=1;

LCD_DATA_PORT |= (command & 0xf0);//高4位不用改

delay_nus(2);

EN=0;

}

void LCD_write_command(uchar command) //写指令

{

LCD_write_cmd(command);

LCD_write_cmd(command<<4);

}

void LCD_write_data(unsigned char data) //写数据

{

delay_nus(10);

RS=1;

EN=0;//使能清零

LCD_DATA_PORT&=0x0f;

EN=1;

LCD_DATA_PORT |= (data & 0xf0);//高4位不用改

delay_nus(2);

EN=0;

delay_nus(2);

LCD_DATA_PORT&=0x0F; //清低四位

EN=1;

LCD_DATA_PORT |= ((data << 4) & 0xf0);//发送低4位

delay_nus(2);

EN=0;

}

void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1

{

LCD_set_xy( X, Y ); //写地址

while (*s) // 写显示字符

{

LCD_write_data( *s );

s ++;

}

}

void LCD_set_xy( unsigned char x, unsigned char y )  //写地址函数

{

unsigned char address;

if (y == 0) address = 0x80 + x;

else        address = 0xc0 + x;

LCD_write_command( address);

}

void delay_nms(unsigned int n) //N ms延时函数

{

uint a,b;

for(a=n;a>0;a--)

for(b=80;b>0;b--);

}

void delay_nus(unsigned int n) //N us延时函数

{

unsigned int i;

for (i=0;i

}

void main()

{

LCD_init();

//    LCD_write_command(0x0d); //光标开

while(1)

{

RD0=1;

delay_nms(1000);

RD0=0;

delay_nms(1000);

LCD_write_string(0,0,"ceshiLCD");

LCD_write_string(0,1,"hahahaha");

delay_nms(2000);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的PIC单片机C语言写入内部EEPROM的代码: ``` #include <xc.h> #define _XTAL_FREQ 4000000 // 设置CPU频率 // 定义EEPROM写入函数 void eeprom_write(unsigned char address, unsigned char data) { EEADR = address; // 设置EEPROM地址 EEDATA = data; // 设置要写入的数据 EECON1bits.EEPGD = 0; // 选择EEPROM数据存储器 EECON1bits.WREN = 1; // 允许EEPROM写入 INTCONbits.GIE = 0; // 禁止全局中断 EECON2 = 0x55; // 写入特定序列 EECON2 = 0xAA; EECON1bits.WR = 1; // 执行写入操作 while(EECON1bits.WR); // 等待写入完成 EECON1bits.WREN = 0; // 禁止EEPROM写入 INTCONbits.GIE = 1; // 允许全局中断 } void main(void) { eeprom_write(0x00, 0x55); // 将0x55写入EEPROM地址0x00 while(1) { // 你的程序 } } ``` 这个代码中,我们首先定义了CPU的频率,然后定义了一个EEPROM写入函数`eeprom_write`,该函数接收两个参数,即要写入的EEPROM地址和要写入的数据。 在`eeprom_write`函数中,我们首先设置EEPROM地址和要写入的数据,然后允许EEPROM写入,并禁止全局中断。接着,我们写入特定的序列,执行写入操作,并等待写入完成。最后,我们禁止EEPROM写入,并允许全局中断。 在`main`函数中,我们调用了`eeprom_write`函数,将0x55写入EEPROM地址为0x00的置。 需要注意的是,上述代码仅适用于PIC单片机内置EEPROM,如果需要写入外部EEPROM,则需要根据具体的芯片手册进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值