当前位置:网站首页>Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
2022-07-02 05:55:00 【I don't know who】
The nixie tube shows ADC、 Serial port display ADC Keys and values
One 、 subject
Through and channel ADC0 Connected 16 individual ADC Measurement of analog voltage signal changes caused by keys , Use the nixie tube on the experimental box 2 Bit display key code , low 4 Bit display AD value . Use serial port 1 Display the key value and conversion result value on the host serial port assistant , Display format is : Key value : Corresponding ADC result . Continue to press enter to switch lines , Display the next key value and its... In the same format ADC result .
Two 、 Code
main function
// high 2 Bit display key code , low 4 Bit display AD value
#include<stc8h.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
bit busy;
u8 key=0;
uchar cnt1ms=0;
uint ad_volume=0;
#define ADC_OFFSET 64
uchar KeyCode=0;
void CalculateAdcKey(uint adc);
uint Get_ADC12bitResult(uchar channel); //channel = 0~7
#define font_PORT P6 // Define font code output port
#define position_PORT P7 // Define bit control code output port
uchar code LED_SEG[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,0x40,0x79,0x24,0x30,0x19,0x12d,0x02,0x78,0x00,0x10,0xbf };
// Definition "0、1、2、3、4、5、6、7、8、9","A、B、C、D、E、F" as well as " destroy " Font code
// Definition "0、1、2、3、4、5、6、7、8、9"( Including decimal point ) And “-” Font code
uchar code Scan_bit[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf, 0xbf, 0x7f}; // Define scan bit control code
uchar data Dis_buf[]={
16,16,16,16,16,16,16,0}; // Define the display buffer , The lowest bit shows "0", Others are " destroy "
void gpio() //gpio Initialize to quasi two-way port , At first, except P30,P31 Others are in high resistance state
{
P0M1 = 0x00; P0M0 = 0x00; // Set as quasi two-way port
P1M1 = 0x00; P1M0 = 0x00; // Set as quasi two-way port
P2M1 = 0x00; P2M0 = 0x00; // Set as quasi two-way port
P3M1 = 0x00; P3M0 = 0x00; // Set as quasi two-way port
P4M1 = 0x00; P4M0 = 0x00; // Set as quasi two-way port
P5M1 = 0x00; P5M0 = 0x00; // Set as quasi two-way port
P6M1 = 0x00; P6M0 = 0x00; // Set as quasi two-way port
P7M1 = 0x00; P7M0 = 0x00; // Set as quasi two-way port
}
/*---------------------------- Send byte ----------------------------*/
void SendData(uchar dat)
{
while (busy);
busy = 1;
SBUF = dat; // The data to be sent is stored SBUF
}
/*---------------------------- Send string function ----------------------------*/
void SendString(uchar *s)
{
while (*s !='\0') // The string is read before it stops
{
SendData(*s++); // Every byte sent s++
}
}
void Timer0Init(void) //1 millisecond @24.000MHz
{
AUXR |= 0x80; // Timer clock 1T Pattern
TMOD &= 0xF0; // Set timer mode
TL0 = 0x40; // Set initial value of timing
TH0 = 0xA2; // Set initial value of timing
TF0 = 0; // eliminate TF0 sign
TR0 = 1; // Timer 0 Start timing
}
/*―――――――――― The time delay function ――――――――――――*/
void Delay1ms() //@24.000MHz
{
unsigned char i, j;
_nop_();
i = 32;
j = 40;
do
{
while (--j);
} while (--i);
}
void Delay500us() //@24.000MHz
{
unsigned char i, j;
i = 16;
j = 147;
do
{
while (--j);
} while (--i);
}
/*―――――――――― Show function ――――――――――――*/
void LED_display(void)
{
uchar i;
for(i=0;i<8;i++)
{
position_PORT =0xff; font_PORT =LED_SEG[Dis_buf[i]]; position_PORT = Scan_bit[7-i]; Delay500us();
}
}
/*―――――――――― Serial initialization ――――――――――――*/
void InitUART(void)
{
SCON = 0x50; //8 Bit data
P_SW1= P_SW1 & 0x3F;
AUXR |= 0x40; // Timer 1T Pattern
AUXR &= 0xFE;
TMOD &= 0x0F;
TMOD |= 0x20; //8 Bit auto reload mode
TL1 = 0xDC; //
TH1 = 0xDC;
TR1 = 1; // Turn on timer 1
ES = 1; // Enable serial port interrupt
EA = 1;
}
/**********************************************/
void main(void)
{
uint j;
gpio();
ADCCFG=ADCCFG|0x20; //RESFMT Location 1, Store results right aligned
ADC_CONTR = 0x80; // open AD Convert module power
P1M1=P1M1|0x01; //P1 Set the port to high resistance
Timer0Init();
InitUART();
ET0 = 1; //Timer0 interrupt enable
TR0 = 1; //Tiner0 run
EA = 1; // Open total interrupt
while(1)
{
Dis_buf[4] = ad_volume / 1000%10; // Show ad value
Dis_buf[5] = ad_volume/100%10; // Show ad value
Dis_buf[6] = ad_volume / 10%10; // Show ad value
Dis_buf[7] = ad_volume % 10; // Show ad value
Dis_buf[0] = KeyCode / 10; // Display key code
Dis_buf[1] = KeyCode % 10; // Display key code
LED_display();
if(cnt1ms >= 10) //10ms Read it once ADC
{
cnt1ms = 0;
j = Get_ADC12bitResult(0); //0 passageway , Make a query ADC, The return value is the result , == 4096 For a mistake
if(((256-ADC_OFFSET)<j)&&(j < 4096))
{
LED_display(); // To shake
LED_display();
j = Get_ADC12bitResult(0);
if(((256-ADC_OFFSET)<j)&&(j < 4096))
{
ad_volume=j;
CalculateAdcKey(j); // Calculation key
SendData(KeyCode/10+0x30);
SendData(KeyCode%10+0x30);
SendString(": ");
SendData(ad_volume / 1000%10+0x30);
SendData(ad_volume/100%10+0x30);
SendData(ad_volume / 10%10+0x30);
SendData(ad_volume % 10+0x30);
SendString("\r\n");
Dis_buf[4] = ad_volume / 1000%10; // Show ad value
Dis_buf[5] = ad_volume/100%10; // Show ad value
Dis_buf[6] = ad_volume / 10%10; // Show ad value
Dis_buf[7] = ad_volume % 10; // Show ad value
Dis_buf[0] = KeyCode / 10; // Display key code
Dis_buf[1] = KeyCode % 10; // Display key code
LED_display();
L1: j = Get_ADC12bitResult(0);
while(((256-ADC_OFFSET)<j)&&(j < 4096)) // Key release
{
LED_display();
goto L1;
}
}
}
}
}
}
/**************** measurement AD value *************************/
uint Get_ADC12bitResult(uchar channel) //channel = 0~15
{
ADC_RES = 0; // Clear the conversion result register 0
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xe0) | 0x40 | channel; // Power on first , To start , Then select the channel
_nop_(); _nop_(); _nop_(); _nop_();// You need to wait for the circuit to stabilize at the beginning for the first time
while((ADC_CONTR & 0x20) == 0) ; // wait for ADC complete
ADC_CONTR &= ~0x20; // eliminate ADC End mark
return (((uint)ADC_RES << 8) | ADC_RESL );
}
/***************** ADC Keyboard calculation key code ********************************/
void CalculateAdcKey(uint adc)
{
uchar i;
uint j=256;
for(i=1; i<=16; i++)
{
if((adc >= (j - ADC_OFFSET)) && (adc <= (j + ADC_OFFSET))) break; // Judge whether it is within the deviation range
j += 256;
}
if(i < 17) KeyCode = i; // Save key code
}
/********************** Timer0 1ms Interrupt function ************************/
void timer0 (void) interrupt 1
{
cnt1ms++;
}
/*---------------------------- UART interrupt -----------------------------*/
void Uart() interrupt 4 using 1
{
LED_display();
if (RI)
{
RI = 0; // After receiving characters ,RI clear 0
}
if (TI)
{
TI = 0; // After sending characters TI clear 0
busy = 0; // After sending one character busy clear 0
}
}
LED_display function
// The following is a LED_display.c
#include <stc8h.h>
#include <intrins.h>
#define font_PORT P6 // Define font code output port
#define position_PORT P7 // Define bit control code output port
uchar code LED_SEG[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,0x40,0x79,0x24,0x30,0x19,0x12d,0x02,0x78,0x00,0x10,0xbf };
// Definition "0、1、2、3、4、5、6、7、8、9","A、B、C、D、E、F" as well as " destroy " Font code
// Definition "0、1、2、3、4、5、6、7、8、9"( Including decimal point ) And “-” Font code
uchar code Scan_bit[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf, 0xbf, 0x7f}; // Define scan bit control code
uchar data Dis_buf[]={
16,16,16,16,16,16,16,0}; // Define the display buffer , The lowest bit shows "0", Others are " destroy "
/*―――――――――― The time delay function ――――――――――――*/
void Delay1ms() //@24.000MHz
{
unsigned char i, j;
_nop_();
i = 32;
j = 40;
do
{
while (--j);
} while (--i);
}
/*―――――――――― Show function ――――――――――――*/
void LED_display(void)
{
uchar i;
for(i=0;i<8;i++)
{
position_PORT =0xff; font_PORT =LED_SEG[Dis_buf[i]]; position_PORT = Scan_bit[7-i]; Delay1ms ();
}
}
LED_display.h The header file
#ifndef __LED_DISPLAY_H__
#define __LED_DISPLAY_H__
void Delay1ms();
void LED_display(void);
uchar code LED_SEG[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,0x40,0x79,0x24,0x30,0x19,0x12d,0x02,0x78,0x00,0x10,0xbf };
// Definition "0、1、2、3、4、5、6、7、8、9","A、B、C、D、E、F" as well as " destroy " Font code
// Definition "0、1、2、3、4、5、6、7、8、9"( Including decimal point ) And “-” Font code
uchar code Scan_bit[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf, 0xbf, 0x7f}; // Define scan bit control code
uchar data Dis_buf[]={
16,16,16,16,16,16,16,0}; // Define the display buffer , The lowest bit shows "0", Others are " destroy "
#end if
Thank you very much for watching !!!
Series articles ——STC8H8K Series compilation 51 actual combat
STC8H8K Series compilation and C51 actual combat —— Realize the running lantern (51 edition )
STC8H8K Series compilation and C51 actual combat —— Realize the running lantern ( Assembly Edition )
STC8H8K Series compilation and C51 actual combat —— Switch control Timer Stopwatch (C51 edition )
STC8H8K Series compilation and C51 actual combat —— Dual interrupt control timer flow lamp
STC8H8K Series compilation and C51 actual combat —— Double interrupt plus and minus counter
STC8H8K Series compilation and C51 actual combat —— Simple frequency meter
STC8H8K Series compilation and C51 actual combat —— Second countdown timer ( Assembly Edition )
STC8H8K Series compilation and C51 actual combat —— Second countdown timer (51 edition )
STC8H8K Series compilation and C51 actual combat —— Keys allow key counts (51 edition )
STC8H8K Series compilation and C51 actual combat —— Keys allow key counts ( Assembly Edition )
边栏推荐
- How to change the IP address of computer mobile phone simulator
- LCD之MIPI协议的一些说明
- Common websites for Postgraduates in data mining
- php父类(parent)
- Some descriptions of Mipi protocol of LCD
- 500. 键盘行
- Eco express micro engine system has supported one click deployment to cloud hosting
- 数据挖掘方向研究生常用网站
- "Simple" infinite magic cube
- A collection of commonly used plug-ins for idea development tools
猜你喜欢
DRM display framework as I understand it
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
软件测试答疑篇
文件包含漏洞(一)
Matplotlib double Y axis + adjust legend position
Lantern Festival gift - plant vs zombie game (realized by Matlab)
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
"Simple" infinite magic cube
File contains vulnerabilities (II)
Oled12864 LCD screen
随机推荐
File contains vulnerabilities (II)
【论文翻译】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
Lantern Festival gift - plant vs zombie game (realized by Matlab)
ESP8266与STC8H8K单片机联动——天气时钟
数理统计与机器学习
15 C language advanced dynamic memory management
PHP 开发与测试 Webservice(SOAP)-Win
Common protocols and download paths of NR
文件包含漏洞(二)
[golang syntax] be careful with the copy of slices
外部中断无法进入,删代码再还原就好......记录这个想不到的bug
死磕大屏UI,FineReport开发日记
我所理解的DRM显示框架
PHP array to XML
1035 Password
Pytorch Basics
深度学习分类网络--VGGNet
PHP parent
JS determines whether the mobile terminal or the PC terminal