仿真图proteus 8.9
程序编译器:keil 5
编程语言:C语言
设计编号:C0055
主要功能:
基于STM32的简易电子秤仿真,读取压力传感器数值,带总价自动计算,使用LCD1602显示。
注意:矩阵键盘不起作用,因为没有时间编程,其它功能正常。
Proteus仿真
程序
main函数:
主要显示当前的压力传感器数值。
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "lcd1602.h"
#include "adc0804.h"
int main(void)
{
u8 adcx = 0;
u8 totalll = 0;
uint8_t adcxxx[4] = {0, 0, 0, 0};
uint8_t total[5] = {0, 0, 0, 0, 0};
LCD_init();
ADC_init();
lcd_delay_ms(1000);
LCD_write_string(0,1," WGT*PRICE=TOTAL");
LCD_write_string(6, 0, "*");
LCD_write_string(10, 0, "=");
while(1)
{
adcx = read_adc0804();
adcxxx[0] = (adcx % 100 / 10) + 0x30;
adcxxx[1] = 46;
adcxxx[2] = (adcx % 10) + 0x30;
adcxxx[3] = (((adcx % 100 / 10) * (adcx % 10)) + (adcx % 10)) % 10 + 0x30;
totalll = ((adcx % 100 / 10) * 10 + (adcx % 10)) * 2;
total[0] = (totalll / 100) + 0x30;
total[1] = (totalll % 100 / 10) + 0x30;
total[2] = 46;
total[3] = (totalll % 10) + 0x30;
LCD_write_string(0, 0, (char*)adcxxx);
LCD_write_string(11, 0, (char*)total);
LCD_write_string(8, 0, "2");
lcd_delay_ms(1000);
}
}
LCD1602显示驱动
#include "lcd1602.h"
#include "delay.h"
#define DELAY_2N 0
void lcd_delay_us(unsigned int t)
{
unsigned int i, j;
for(i = 10; i > 0; i--)
for(j = t; j > 0; j--);
}
void lcd_delay_ms(unsigned int t)
{
unsigned int i;
for(i = t; i > 0; i--)
lcd_delay_us(10);
}
//==================================================
void LCD_init(void)
{
/*********************液晶使用的I/O口初始化**************************/
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2| GPIO_Pin_0| GPIO_Pin_3
| GPIO_Pin_4| GPIO_Pin_5| GPIO_Pin_6| GPIO_Pin_7
| GPIO_Pin_8| GPIO_Pin_9| GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
LCD_RW(0); //读写位直接低电平,只写不读
/*********************液晶初始化**************************/
lcd_delay_us(340);
LCD_RS(0);
LCD_write_cmd(0x38); // 8bit显示模式,2行,5x7字体
lcd_delay_ms(4);
LCD_write_cmd(0x08); // 显示关闭
lcd_delay_ms(4);
LCD_write_cmd(0x01); // 显示清屏
lcd_delay_ms(4);
LCD_write_cmd(0x06); // 显示光标移动设置
lcd_delay_ms(4);
LCD_write_cmd(0x0c); // 显示开,光标开,光标闪烁
lcd_delay_ms(4);
LCD_write_cmd(0x01); //清屏
lcd_delay_ms(4);
}
/*--------------------------------------------------
函数说明:写命令到液晶
---------------------------------------------------*/
void LCD_write_cmd(unsigned char cmd)
{
LCD_RS(0);
LCD_Write_byte(cmd);
lcd_delay_us(340);
}
/*--------------------------------------------------
函数说明:写数据到液晶
---------------------------------------------------*/
void LCD_write_data(unsigned char w_data)
{
LCD_RS(1);
LCD_Write_byte(w_data);
lcd_delay_us(340);
}
/*--------------------------------------------------
函数说明:写4bit到液晶
--------------------------------------------------*/
void LCD_Write_byte(unsigned char num)
{
if (num&0x01)
data0(1);
else
data0(0);
if (num&0x02)
data1(1);
else
data1(0);
if (num&0x04)
data2(1);
else
data2(0);
if (num&0x08)
data3(1);
else
data3(0);
if (num&0x10)
data4(1);
else
data4(0);
if (num&0x20)
data5(1);
else
data5(0);
if (num&0x40)
data6(1);
else
data6(0);
if (num&0x80)
data7(1);
else
data7(0);
lcd_delay_us(340);
LCD_EN(1);
lcd_delay_us(340);
LCD_EN(0);
lcd_delay_us(340);
}
/*----------------------------------------------------
LCD_set_xy : 设置LCD显示的起始位置
输入参数:x、y : 显示字符串的位置,X:0-15,Y:0-1
-----------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address = 0;
if (y==0)
{
address=0x80+x;
}
else
{
address=0xc0+x;
}
// y ? (address=0xc0+x): (address=0x80+x) ;
LCD_write_cmd(address);
}
/*---------------------------------------------------
LCD_write_string : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置
---------------------------------------------------*/
void LCD_write_string(unsigned char X,unsigned char Y, char *s)
{
LCD_set_xy(X,Y);
while (*s)
{
LCD_write_data(*s);
s++;
}
}
//=======================================================
void LCD_wstring(unsigned char X,unsigned char *s)
{
LCD_write_cmd(X);
while (*s)
{
LCD_write_data(*s);
s++;
}
}
资料设计
下载
阅读全文