當前位置:編程學習大全網 - 編程語言 - 產生四種方波:1.通斷各為0.35S的方波;2.通斷各為0.1S的方波;為通1S斷4S的方波;4.恒為高電平的波。

產生四種方波:1.通斷各為0.35S的方波;2.通斷各為0.1S的方波;為通1S斷4S的方波;4.恒為高電平的波。

剛剛寫了下,具體如下,仿真OK,開機不動作,按鍵後動作,S52單片機

#include <reg52.h>

#define TRUE 1

/********************************************

晶振:12MHZ

KEY : P1.0

********************************************/

//typedef unsigned char uchar;

//typedef unsigned int uint;

sbit IO = P1^0;

sbit K = P1^1;

uchar state = 10;

uchar on_time = 0;

uchar off_time = 0;

bit key_flag = 0;

uchar onoff = 0;

void Timer0_init(void)

{

TMOD = 0X01;

ET0 = 1;

EA = 1;

TR0 = 1;

}

uchar Key_scan(void)

{

static uchar old_key = 0;

static uchar new_key = 0;

static uchar key_buf = 0;

uchar key = 0;

new_key = (uchar)K;

if(new_key == old_key)

{

if(new_key != 0)

{

key_buf = 1;

}

}

else

{

if(key_buf)

{

key = TRUE;

key_buf = 0;

}

}

old_key = new_key ;

return key;

}

void main(void)

{

Timer0_init();

while(1)

{

if(key_flag)

{

key_flag = 0;

if(Key_scan() == 1)

{

state ++;

if(state == 4 || state == 11)state = 0;

switch(state)

{

case 0:on_time = 7; off_time = 7; break;

case 1:on_time = 3; off_time = 3; break;

case 2:on_time = 20;off_time = 80;break;

case 3:break;

default:break;

}

}

}

}

}

void Timer0_isovf(void)interrupt 1

{

static uchar cnt = 0;

static bit flag = 0;

TH0 = 0X3c; //50ms

TL0 = 0xb0;

key_flag = 1;

if(state == 3)

{

IO = 1;

cnt = 0;

}

if(cnt >= onoff && state < 10)

{

cnt = 0;

if(flag)

{

flag = 0;

onoff = off_time;

IO = 0;

}

else

{

flag = 1;

onoff = on_time;

IO = 1;

}

}

cnt ++;

}

  • 上一篇:如何報名華為自研編程
  • 下一篇:初中生輟學了,學什麽技術好?
  • copyright 2024編程學習大全網