微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 关于51单片机存储16位数据的问题

关于51单片机存储16位数据的问题

时间:10-02 整理:3721RD 点击:
51单片机存储16位数据,用5110显示,但是我存的是1234,显示是1279,哪位高手给指导一下,非常感谢#include<reg51.h>
#include<intrins.h>         
#include<stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_RST=P1^0; // N5110 的复位信号引脚,
sbit LCD_CE=P1^1 ;// N5110 的片选信号引脚,
sbit LCD_DC=P1^2; // N5110 的数据/命令选择引脚,
sbit LCD_CLK=P1^3; // N5110 的同步时钟信号输入引脚,
sbit LCD_DIN=P1^4;// N5110 的数据输入引脚
sbit LCD_BL=P1^5; // N5110 的背光灯控制引脚,
sbit SDA=P2^0;
sbit SCL=P2^1;
uint a=1234;
int i,j;
uchar shuzhi[]={0};
code unsigned char ascii_dot[] = // ASCII 表
{
0x00,0x00,0x00,0x00,0x00, // - -
0x00,0x00,0x5F,0x00,0x00, // -!-
0x00,0x07,0x00,0x07,0x00, // -"-
0x14,0x7F,0x14,0x7F,0x14, // -#-
0x24,0x2E,0x7B,0x2A,0x12, // -$-
0x23,0x13,0x08,0x64,0x62, // -%-
0x36,0x49,0x56,0x20,0x50, // -&-
0x00,0x04,0x03,0x01,0x00, // -'-
0x00,0x1C,0x22,0x41,0x00, // -(-
0x00,0x41,0x22,0x1C,0x00, // -)-
0x22,0x14,0x7F,0x14,0x22, // -*-
0x08,0x08,0x7F,0x08,0x08, // -+-
0x40,0x30,0x10,0x00,0x00, // -,-
0x08,0x08,0x08,0x08,0x08, // ---
0x00,0x60,0x60,0x00,0x00, // -.-
0x20,0x10,0x08,0x04,0x02, // -/-
0x3E,0x51,0x49,0x45,0x3E, // -0-
0x00,0x42,0x7F,0x40,0x00, // -1-
0x62,0x51,0x49,0x49,0x46, // -2-
0x21,0x41,0x49,0x4D,0x33, // -3-
0x18,0x14,0x12,0x7F,0x10, // -4-
0x27,0x45,0x45,0x45,0x39, // -5-
0x3C,0x4A,0x49,0x49,0x31, // -6-
0x01,0x71,0x09,0x05,0x03, // -7-
0x36,0x49,0x49,0x49,0x36, // -8-
0x46,0x49,0x49,0x29,0x1E, // -9-
0x00,0x36,0x36,0x00,0x00, // -:-
0x40,0x36,0x36,0x00,0x00, // -;-
0x08,0x14,0x22,0x41,0x00, // -<-
0x14,0x14,0x14,0x14,0x14, // -=-
0x00,0x41,0x22,0x14,0x08, // ->-
0x02,0x01,0x59,0x05,0x02, // -?-
0x3E,0x41,0x5D,0x55,0x5E, // -@-
0x7C,0x12,0x11,0x12,0x7C, // -A-
0x7F,0x49,0x49,0x49,0x36, // -B-
0x3E,0x41,0x41,0x41,0x22, // -C-
0x7F,0x41,0x41,0x41,0x3E, // -D-
0x7F,0x49,0x49,0x49,0x41, // -E-
0x7F,0x09,0x09,0x09,0x01, // -F-
0x3E,0x41,0x51,0x51,0x72, // -G-
0x7F,0x08,0x08,0x08,0x7F, // -H-
0x00,0x41,0x7F,0x41,0x00, // -I-
0x20,0x40,0x41,0x3F,0x01, // -J-
0x7F,0x08,0x14,0x22,0x41, // -K-
0x7F,0x40,0x40,0x40,0x40, // -L-
0x7F,0x02,0x0C,0x02,0x7F, // -M-
0x7F,0x04,0x08,0x10,0x7F, // -N-
0x3E,0x41,0x41,0x41,0x3E, // -O-
0x7F,0x09,0x09,0x09,0x06, // -P-
0x3E,0x41,0x51,0x21,0x5E, // -Q-
0x7F,0x09,0x19,0x29,0x46, // -R-
0x26,0x49,0x49,0x49,0x32, // -S-
0x01,0x01,0x7F,0x01,0x01, // -T-
0x3F,0x40,0x40,0x40,0x3F, // -U-
0x1F,0x20,0x40,0x20,0x1F, // -V-
0x7F,0x20,0x18,0x20,0x7F, // -W-
0x63,0x14,0x08,0x14,0x63, // -X-
0x03,0x04,0x78,0x04,0x03, // -Y-
0x61,0x51,0x49,0x45,0x43, // -Z-
0x7F,0x7F,0x41,0x41,0x00, // -[-
0x02,0x04,0x08,0x10,0x20, // -\-
0x00,0x41,0x41,0x7F,0x7F, // -]-
0x04,0x02,0x7F,0x02,0x04, // -^-
0x08,0x1C,0x2A,0x08,0x08, // -_-
0x00,0x00,0x01,0x02,0x04, // -`-
0x24,0x54,0x54,0x38,0x40, // -a-
0x7F,0x28,0x44,0x44,0x38, // -b-
0x38,0x44,0x44,0x44,0x08, // -c-
0x38,0x44,0x44,0x28,0x7F, // -d-
0x38,0x54,0x54,0x54,0x08, // -e-
0x08,0x7E,0x09,0x09,0x02, // -f
0x98,0xA4,0xA4,0xA4,0x78,// -g-
0x7F,0x08,0x04,0x04,0x78, // -h-
0x00,0x00,0x79,0x00,0x00, // -i-
0x00,0x80,0x88,0x79,0x00, // -j-
0x7F,0x10,0x28,0x44,0x40, // -k-
0x00,0x41,0x7F,0x40,0x00, // -l-
0x78,0x04,0x78,0x04,0x78, // -m-
0x04,0x78,0x04,0x04,0x78, // -n-
0x38,0x44,0x44,0x44,0x38, // -o-
0xFC,0x24,0x24,0x24,0x18, // -p-
0x18,0x24,0x24,0x24,0xFC, // -q-
0x04,0x78,0x04,0x04,0x08, // -r-
0x48,0x54,0x54,0x54,0x24, // -s-
0x04,0x3F,0x44,0x44,0x24, // -t-
0x3C,0x40,0x40,0x3C,0x40, // -u-
0x1C,0x20,0x40,0x20,0x1C, // -v-
0x3C,0x40,0x3C,0x40,0x3C, // -w-
0x44,0x28,0x10,0x28,0x44, // -x-
0x9C,0xA0,0xA0,0x90,0x7C, // -y-
0x44,0x64,0x54,0x4C,0x44, // -z-
0x08,0x36,0x41,0x00,0x00, // -{-
0x00,0x00,0x77,0x00,0x00, // -|-
0x00,0x00,0x41,0x36,0x08, // -}-
0x06,0x09,0x09,0x06,0x00, // -°-
0x08,0x04,0x08,0x10,0x08, // -~-
0x55,0x2A,0x55,0x2A,0x55, // - -
};       
void LCD_write_byte(unsigned char dat, unsigned char command) //读取串口数据
{
unsigned char i;
LCD_CE = 0;
if (command == 0)
LCD_DC = 0; // 0 命令
else
LCD_DC = 1; //1 数据
for(i=0;i<8;i++)
{
if(dat&0x80) //取数据的最高位,1 或0
LCD_DIN = 1; //(串行数据输入)
else
LCD_DIN= 0;
LCD_CLK = 0;
dat = dat << 1; //左移,依次将数据读入
LCD_CLK = 1;
}
LCD_CE = 1; //关闭液晶屏
}
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_byte(0x80|X,0); // row 行
LCD_write_byte(0x40|Y,0); // column 列
}
/*******清屏函数**********/
void LCD_clear(void)
{
unsigned int i ;
LCD_set_XY(0,0); // 设置初始坐标为0,0
for(i=0;i<504;i++) // 写84*6 组0x00 数据完成清屏
{
LCD_write_byte(0x00,1);
}
}
/*******初始化函数**********/
// N5110 写一个字符函数
void Write_Char(char ascii)
{
unsigned int i,coord;
coord=(ascii-' ')*5 ; // 计算该字符的模在ASCII 码表的位置
for(i=0;i<5;i++) // 依次写入模值
{
LCD_write_byte(ascii_dot[coord+i],1);
}
}
// N5110 写一个字符串函数
void Write_String(char *string)
{
while(*string != 0x00) // 输出直到遇到0x00
{
Write_Char(*string) ;
string++ ;
}
}
void LCD_Init(void) //初始化液晶屏,用八位的二进制数控制各引脚
{
LCD_RST=0; //低电平,复位
_nop_(); //空循环一个机器周期
LCD_RST=1;
LCD_CE=1; //// 初始不选通N5110 芯片
LCD_write_byte(0x21, 0);// 使用扩展命令设置LCD 模式
LCD_write_byte(0xbb, 0); // 设置偏置电压
LCD_write_byte(0x06, 0); // 温度校正
LCD_write_byte(0x13, 0); // 1:48
LCD_write_byte(0x20, 0); // 使用基本命令
// 清屏
LCD_write_byte(0x0c, 0);// 设定显示模式,正常显示
LCD_clear();
}
void delay(){
;;
}
void delayms(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
void start() //开始信号
{
        SDA=1;
        delay();
        SCL=1;
        delay();
        SDA=0;
        delay();
}
void stop() //停止
{
        SDA=0;
        delay();
        SCL=1;
        delay();
        SDA=1;
        delay();
}
void respons() //应答
{
        uchar i;
        SCL=1;
        delay();
        while((SDA==1)&&(i<250))i++;
        SCL=0;
        delay();
}
void c02init()
{
        SDA=1;
        delay();
        SCL=1;
        delay();
}
void write_byte(uchar date)
{
        uchar i,temp;
        temp=date;
        for(i=0;i<8;i++)
        {
                temp=temp<<1;
                SCL=0;
                delay();
                SDA=CY;
                delay();
                SCL=1;
                delay();
        }
        SCL=0;
        delay();
        SDA=1;
        delay();
}
uchar read_byte()
{
        uchar i,k;
        SCL=0;
        delay();
        SDA=1;
        delay();
        for(i=0;i<8;i++)
        {
                SCL=1;
                delay();
                k=(k<<1)|SDA;
                SCL=0;
                delay();
        }
        return k;
}
void write_add(uchar address,uchar date)
{
        start();
        write_byte(0xa0);
        respons();
        write_byte(address);
        respons();
        write_byte(date);
        respons();
        stop();
}
uchar read_add(uchar address)
{
        uchar date;
        start();
        write_byte(0xa0);
        respons();
        write_byte(address);
        respons();
        start();
        write_byte(0xa1);
        respons();
        date=read_byte();
        stop();
        return date;
}
void main()
{
        LCD_Init();
        c02init();
       
/*        i=a/256;
        j=a%256;
        write_add(0,i);
        write_add(1,j);          */        //这段用于先存入数据,存的过程中,把下面的屏蔽
        i=read_add(0);                        // 这段用于读出数据显示
        j=read_add(1);
        a=((i*256)|j);
        LCD_set_XY(0,0);
        sprintf(shuzhi,"%d",a);
        Write_String(shuzhi);  
        while(1);

}

怎么没人回答呢,问题太简单了?

有没有高手给指点一下?

存储芯片存储数据,24c02,第一部分是5110的函数,第二部分是24c02的函数,数据存入24c02,之后再取出来

不知小编解决问题了没
建议字符串变量定义的时候写成这样
uchar shuzhi[];  或这样 uchar shuzhi[]={};
里面不要赋值试试。你里面加0就定义长度为1了

另外,建议小编地址数据最好写成16进制形式,这样直观、易读、准确些

不是这个原因,我试了你的方法,uchar shuzhi[]在5110上显示是乱码,uchar shuzhi[]={}在keil里编译错误,也不是十六位的原因。我刚才摸索了一下,存入地址是0和1的时候,0地址数据存储和提取是正常的,但是1地址提取出的数值是255,24c02没存储之前都是255,说明1地址没有存入值,之后在两个存储语句之间延时了一下,显示结果正常。谢谢你的回答,因为你的回答,我又把电路搭了一遍,最后找到的原因。最后非常感谢

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top