#include <stdlib.h>
#include <string.h>
char atohex(char s)
{
char n = s
if( n >= 'a' &&n <= 'z')
n = n + 'A' - 'a'
if(n >源扒 0x39) //alpha
return (n - 0x37)
else //number
return (n - 0x30)
}
unsigned char chartohex(unsigned char *str)
{
unsigned char code[3]
unsigned char temp
memset(code, 0, 3)
memcpy(code, str, 2)
temp = atohex(code[0])
printf("the temp is %x\n", temp)
temp = (temp <<4) &0xF0
printf("the temp is %x\n", temp)
temp |= atohex(code[1])
printf("the code is %s\n", code)
/卜裂隐/printbinary(temp)
printf("the temp is %x, the sizeof(temp) is %d\n", temp, sizeof(temp))
return temp
}
int main()
{
unsigned char pstr[] = "aB"
unsigned char c, z
c = chartohex(pstr)
printf("The c hex is %x\n", c)
z = c
printf("The c dec is %d\型厅n", z)
return 0
}
#include"reg52.h"
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^1//定义按键
sbit key2=P1^2//定义按键
uint num
/*1ms廷时*/
void delay_ms(uint z)
{
uint i,j
for(i=0i<zi++)
for(j=0j<110j++)
}
/*定时器初始化*/
void init_timer0()
{
TMOD=0x01
TH0=(65535-2000)/256
TL0=(65535-2000)%256
EA=0
ET0=1
TR0=1
}
void main()
{
BELL=0
init_timer0()
while(1)
{
if(key1==0)
{
delay_ms(10) 穗乎镇 //延时消抖
if(key1==0) //再次判断按键是否真的被按下
{
while(key1==0) //等待释放按键
EA=1 //开启总中断
}
}
if(key2==0)
{
delay_ms(10) //延时消抖
if(key2==0) //再次判断按键是否真的被按下
{
while(key2==0) //等待释顷并放按键
EA=0 //关闭总中断
BELL=0 //关闭蜂鸣器
}
}
}
}
void timer0() interrupt 1
{
TH0=(65535-2000)/256
TL0=(65535-2000)%256 //中断计时2ms(500Hz),之所以猜粗用中断是因为定时比较准确 想改延时就把里面的2000改掉,这个数除以1000得的数就是多少毫秒
num++
if(num==1) //让蜂鸣器工以 500Hz频率工作
{
num=0
BELL=~BELL
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)