今天捣鼓了一下AD,采样光敏电阻,用HT1621显示采样值。
代码:
#include "SC_Init.h" //MCU Init header,Include all IC resource headers
#include "SC_it.h"
#include "..\Drivers\SCDriver_list.h"
#include "HeadFiles\SysFunVarDefine.h"
/**************************************Generated by EasyCodeCube*************************************/
uint16_t temp;
#define Smg_a 0x80
#define Smg_b 0x40
#define Smg_c 0x20
#define Smg_d 0x01
#define Smg_e 0x02
#define Smg_f 0x08
#define Smg_g 0x04
#define Smg_dp 0x10
#define Bmp0Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f
#define Bmp1Map Smg_b | Smg_c
#define Bmp2Map Smg_a | Smg_b | Smg_d | Smg_e | Smg_g
#define Bmp3Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_g
#define Bmp4Map Smg_b | Smg_c | Smg_f | Smg_g
#define Bmp5Map Smg_a | Smg_c | Smg_d | Smg_f | Smg_g
#define Bmp6Map Smg_a | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define Bmp8Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define Bmp9Map Smg_a | Smg_b | Smg_c | Smg_d | Smg_f | Smg_g
#define Bmp7Map Smg_a | Smg_b | Smg_c
#define BmpAMap Smg_a | Smg_b | Smg_c | Smg_e | Smg_f | Smg_g
#define BmpBMap Smg_c | Smg_d | Smg_e | Smg_f | Smg_g
#define BmpCMap Smg_a | Smg_d | Smg_e | Smg_f
#define BmpDMap Smg_b | Smg_c | Smg_d | Smg_e | Smg_g
#define BmpEMap Smg_a | Smg_d | Smg_e | Smg_f | Smg_g
#define BmpFMap Smg_a | Smg_e | Smg_f | Smg_g
#define CS_0 GPIO_WriteLow(GPIO2, GPIO_PIN_7)
#define CS_1 GPIO_WriteHigh(GPIO2, GPIO_PIN_7)
#define WR_0 GPIO_WriteLow(GPIO0, GPIO_PIN_0)
#define WR_1 GPIO_WriteHigh(GPIO0, GPIO_PIN_0)
#define DA_0 GPIO_WriteLow(GPIO0, GPIO_PIN_1)
#define DA_1 GPIO_WriteHigh(GPIO0, GPIO_PIN_1)
const unsigned char SegCode[] = {Bmp0Map,Bmp1Map,Bmp2Map,Bmp3Map,Bmp4Map,Bmp5Map,Bmp6Map,Bmp7Map,Bmp8Map,Bmp9Map,BmpAMap,BmpBMap,BmpCMap,BmpDMap,BmpEMap,BmpFMap};
unsigned char DpyNum[6] = {Smg_g,Smg_g,Smg_g,Smg_g,Smg_g,Smg_g}; //显示缓冲区
unsigned int i;
void COMMAND_HT1621(unsigned char comm)
{
unsigned char i;
CS_0;
WR_0;
for(i=0;i<3;i++) //写命令
{
if((0x04<<i)&0x04) DA_1; //100
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<9;i++) //写命令数据
{
if((comm<<i)&0x80) DA_1; //100
else DA_0;
WR_0;
WR_1;
}
CS_1;
}
void HT1621_Display(void)
{
unsigned char i;
CS_0; //片选CS
WR_0;
for(i=0;i<3;i++) //写命令
{
if((0x05<<i)&0x04) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<6;i++) //写地址
{
if((20<<i)&0x20) DA_1; //20对应 SEG地址
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++) //写数据
{
if((DpyNum[5]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++) //写数据
{
if((DpyNum[4]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++) //写数据
{
if((DpyNum[3]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++) //写数据
{
if((DpyNum[2]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++) //写数据
{
if((DpyNum[1]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
for(i=0;i<8;i++) //写数据
{
if((DpyNum[0]<<i)&0x80) DA_1;
else DA_0;
WR_0;
WR_1;
}
CS_1; //释放CS
}
void LCD_Init(void)
{
COMMAND_HT1621(0x01); //SYS EN
COMMAND_HT1621(0x03); //LCD ON
COMMAND_HT1621(0x29); //4 COM 1/3 BIAS
HT1621_Display(); //clear screen
}
/*************************************.Generated by EasyCodeCube.************************************/
/*****************************************************************************************************
* Function Name: main
* Description : This function implements main function.
* Arguments : None
* Return Value : None
******************************************************************************************************/
void main(void)
{
uint16_t qian,bai,shi,ge;
long int i;
/*<Generated by EasyCodeCube begin>*/
/*<UserCodeStart>*//*<SinOne-Tag><3>*/
SC_Init(); /*** MCU init***/
/*<UserCodeEnd>*//*<SinOne-Tag><3>*/
/*<UserCodeStart>*//*<SinOne-Tag><4>*/
/*****MainLoop*****/
LCD_Init();
DpyNum[0] = Smg_a|Smg_d|Smg_g; //"三"
DpyNum[1] = SegCode[0];
DpyNum[2] = SegCode[0];
DpyNum[3] = SegCode[0];
DpyNum[4] = SegCode[0];
DpyNum[5] = SegCode[0];
HT1621_Display();
while(1)
{
/*<UserCodeStart>*//*<SinOne-Tag><5>*/
/*****User program*****/
/*<UserCodeEnd>*//*<SinOne-Tag><5>*/
/*<Begin-Inserted by EasyCodeCube for Condition>*/
ADC_StartConversion();
while((ADCCON&0x20)==0);
temp=ADC_GetConversionValue();
qian=temp/1000;
bai=temp%1000/100;
shi=temp%100/10;
ge=temp%10;
DpyNum[2] = SegCode[qian];
DpyNum[3] = SegCode[bai];
DpyNum[4] = SegCode[shi];
DpyNum[5] = SegCode[ge];
HT1621_Display();
for(i=0;i<500000;i++);
}
/*<UserCodeEnd>*//*<SinOne-Tag><4>*/
/*<Generated by EasyCodeCube end>*/
}
初始化代码:
/*****************************************************
*函数名称: SC_Init
*函数功能: MCU初始化函数
*入口参数:void
*出口参数:void
*****************************************************/
void SC_Init(void)
{
SC_GPIO_Init();
SC_OPTION_Init();
SC_ADC_Init();
/*write initial function here*/
EA = 1;
}
/*****************************************************
*函数名称: SC_GPIO_Init
*函数功能: GPIO初始化函数
*入口参数:void
*出口参数:void
*****************************************************/
void SC_GPIO_Init(void)
{
/*GPIO_Init write here*/
GPIO_Init(GPIO2, GPIO_PIN_7,GPIO_MODE_OUT_PP);
GPIO_Init(GPIO0, GPIO_PIN_0,GPIO_MODE_OUT_PP);
GPIO_Init(GPIO0, GPIO_PIN_1,GPIO_MODE_OUT_PP);
}
/*****************************************************
*函数名称: SC_ADC_Init
*函数功能: ADC初始化函数
*入口参数:void
*出口参数:void
*****************************************************/
void SC_ADC_Init(void)
{
ADC_Init(ADC_PRESSEL_3CLOCK,ADC_Cycle_Null);
/*AIN0口模式设置*/ADC_EAINConfig(ADC_EAIN_0,ENABLE);
ADC_ChannelConfig(ADC_CHANNEL_0,ENABLE);
ADC_VrefConfig(ADC_VREF_VDD);
ADC_ITConfig(ENABLE,LOW);
ADC_Cmd(ENABLE);
/*ADC_Init write here*/
}
效果图:
|