仿真圖proteus 8.9
程序編譯器:keil 5
編程語言:C語言
設(shè)計(jì)編號(hào):C0055
主要功能:
基于STM32的簡易電子秤仿真,讀取壓力傳感器數(shù)值,帶總價(jià)自動(dòng)計(jì)算,使用LCD1602顯示。
注意:矩陣鍵盤不起作用,因?yàn)闆]有時(shí)間編程,其它功能正常。
Proteus仿真
程序
main函數(shù):
主要顯示當(dāng)前的壓力傳感器數(shù)值。
#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顯示驅(qū)動(dòng)
#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); // 顯示關(guān)閉
lcd_delay_ms(4);
LCD_write_cmd(0x01); // 顯示清屏
lcd_delay_ms(4);
LCD_write_cmd(0x06); // 顯示光標(biāo)移動(dòng)設(shè)置
lcd_delay_ms(4);
LCD_write_cmd(0x0c); // 顯示開,光標(biāo)開,光標(biāo)閃爍
lcd_delay_ms(4);
LCD_write_cmd(0x01); //清屏
lcd_delay_ms(4);
}
/*--------------------------------------------------
函數(shù)說明:寫命令到液晶
---------------------------------------------------*/
void LCD_write_cmd(unsigned char cmd)
{
LCD_RS(0);
LCD_Write_byte(cmd);
lcd_delay_us(340);
}
/*--------------------------------------------------
函數(shù)說明:寫數(shù)據(jù)到液晶
---------------------------------------------------*/
void LCD_write_data(unsigned char w_data)
{
LCD_RS(1);
LCD_Write_byte(w_data);
lcd_delay_us(340);
}
/*--------------------------------------------------
函數(shù)說明:寫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 : 設(shè)置LCD顯示的起始位置
輸入?yún)?shù):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 : 英文字符串顯示函數(shù)
輸入?yún)?shù):*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++;
}
}
資料設(shè)計(jì)
下載
閱讀全文