先贴代码:
#include<stc12c5a60s2.h>
#include<intrins.h>
#include<stdio.h>
#include"LCD1602.h"
#define uchar unsigned char
#define uint unsigned int
#define FOSC 11059200L //选择外部晶振
#define BAUD 9600
typedef unsigned char BYTE;
typedef unsigned int WORD;
/*申报相关SFR ADC*/
//sfr ADC_CONTR = 0xBC; //ADC control register ADC控制寄存器地址为BCH
//sfr ADC_RES = 0xBD; //ADC high 8-bit result register ADC转换结果寄存器高八位地址
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
//sfr P1ASF = 0x9D; //P1 secondary function control register P1辅助控制寄存器地址
/*定义常量的ADC的若干ADC操作*/
#define ADC_POWER 0x80 //打开AD转换开关
#define ADC_FLAG 0x10 //模数转换结束标志位
#define ADC_START 0x08 //模数转换启动控制位
#define ADC_SPEEDLL 0x00 //420 clocks
#define ADC_SPEEDL 0x20 //280 clocks
#define ADC_SPEEDH 0x40 //140 clocks
#define ADC_SPEEDHH 0x60 //70 clocks
void LCD();
void Init_Uart();
void InitADC();
void SendData(BYTE dat);
BYTE GetADCResult(BYTE ch);
void Delay(WORD n);
void ShowResult(BYTE ch);
void ShowResult(BYTE ch)
{
SendData(ch); //Show Channel NO.
SendData(GetADCResult(ch)); //Show ADC high 8-bit result
//if you want show 10-bit result, uncomment next line
// SendData(ADC_LOW2); //Show ADC low 2-bit result
}
/*延时函数*/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
/*得到ADC 结果*/
BYTE GetADCResult(BYTE ch)
{
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
_nop_(); //Must wait before inquiry
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
return ADC_RES; //Return ADC result
}
/* SendData发送数据*/
void SendData(BYTE dat)
{
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
SBUF = dat; //Send current data
}
/*ADC初始化*/
void ADC_init()
{
P1ASF = 0x01; //Open 8 channels ADC function
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
Delay(2); //ADC power-on and delay
}
/*初始化 UART 通用异步收发传输器*/
void Uart_init()
{
SCON = 0x5a; //8 bit data ,no parity bit 串口控制寄存器
TMOD = 0x20; //T1 as 8-bit auto reload
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
TR1 = 1; //T1 start running
}
/*LCD程序*/
void lcd()
{
LCD_init();
DelayMs(15); //延时保证信号稳定
sprintf(Test1,"angle:%04d",ADC_RES); //打印输出第一行信息 //16位空格
ShowString(0,Test1);
}
void main()
{ uchar i;
Uart_init(); //Init UART, use to show ADC result
ADC_init(); //Init ADC sfr
while (1)
{
for(i=0;i<10;i++)
{
LCD();
Delay(2);
}
ShowResult(0); //Show Channe0
}
}
#ifndef _LCD1602_H_
#define _LCD1602_H_
#define uchar unsigned char
#define uint unsigned int
#define Data P0//数据端口
sbit RS = P2^4; //Pin4 数据/命令选择位定义
sbit RW = P2^5; //Pin5 读写选择位
sbit E = P2^6; //Pin6 使能信号
sbit wela=P2^7;//数码管锁存器
void write_com();
char data TimeNum[16];
char data Test1[16];
/**********延时函数*****************/
/*微秒*/
void DelayUs(unsigned char us)//delay us
{
unsigned char uscnt;
uscnt=us>>1; /*12MHz频率*/
while(--uscnt);
}
/*毫秒*/
void DelayMs(unsigned char ms)
{
while(--ms)
{
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
/************写入命令函数************/
void write_com(uchar C)
{
DelayMs(5);//操作前短暂延时,保证信号稳定
E=1;
Data=C;
E=0;
}
/*************写入数据函数**************/
void write_data(uchar D)
{
DelayMs(5); //操作前短暂延时,保证信号稳定
RS=1;
E=1;
Data=D;
E=0;
RS=0;
}
/**********写入字符串函数**********/
void ShowString (unsigned char line,char *ptr)
{
unsigned char p;
if (line==0)
p=0x80; //是第二行则命令代码高4位为0xc
else
p=0xc0; //是第二行则命令代码高4位为0x8
write_com (p);//写命令
for (;*ptr!=' ';ptr++)
{
write_data (*ptr); //写数据
}
}
/***********LCD初始化************/
void LCD_init()
{
wela=0;
E=0;
RS=0;
RW=0;
write_com(0x38) ;//display mode
write_com(0x38) ;//display mode
write_com(0x38) ;//display mode
write_com(0x80);
write_com(0x06) ;//显示光标移动位置
write_com(0x0c) ;//显示开及光标设置
write_com(0x01) ;//显示清屏
}
#endif
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
void SendData(BYTE dat)
{
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
SBUF = dat; //Send current data
}
这是干嘛的程序?
一周热门 更多>