2019年10月27日 星期日

106年 v2

#include <at89x52.h>
#define ON 0
#define OFF 1
sbit sw1=P1^0;
sbit LED=P2^0;
bit release_flag;
bit up_flag;
bit led_flag;
bit change_flag;
bit runpwm_flag;
char reset_P0table[]={0xFE, 0XFC, 0XF8, 0XF0, 0XE0, 0XC0, 0X00, 0xc0};
/*               a     ab   abc   abcd  abcde abcdef
 1111 1110 a
 1111 1100 ab
 1111 1000 abc
 1111 0000 abcd
 1110 0000 abcde
 1100 0000 abcdef
 0000 0000 abcdefg
 1100 0000 abcdef
*/
char disp_seg7[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x89,0xc9,0xb6,};
int count;
int count1;
int duty_count;
int pwm_count;
int rpt_count;
char function;
char sw_dly=50;
unsigned int timer;
char duty;
void delay_1ms(int k)
{
int i,j;
for(i=0;i<k;i++)
  for(j=0;j<114;j++)
       ;
}
void cck_run_7seg()//逆時針a ab abc...
{
int i;
for(i=0;i<8;i++)
{
P0=reset_P0table[i];
delay_1ms(500);
}
}
char sw_scan()
{
char sw_value,temp;
sw_value=0;
temp=~P1&0x03;
if(temp) //不等於0就是有按鍵
{
if(release_flag || rpt_count>50)
{
release_flag=0;
sw_value=temp;
}
else{ rpt_count++;}
}
else
{
release_flag=1; //按鍵放開
rpt_count=0;
}
delay_1ms(sw_dly);
return sw_value;
}
void init()
{
TMOD=0X11;
TH0=(65536-400)/256;
TL0=(65536-400)%256;
TH1=(65536-40000)/256;
TL1=(65536-40000)%256;
IE=0X8a;
TR1=1;
IP=0X02;
}
main()
{
char temp;
P0=0xff;
P1=0xff;
init();
cck_run_7seg();
  while(1)
{
temp=sw_scan();
if(temp==1)
{
up_flag=0;
runpwm_flag=0;
TR0=1;
function++;
if(function>11) { function=1; }
     if(function==1)  {up_flag=1; LED=OFF; TR0=0;}
else if(function==2) {duty=1; P0=disp_seg7[duty];}
else if(function==3) {duty=2; P0=disp_seg7[duty];}
else if(function==4) {duty=3; P0=disp_seg7[duty];}
else if(function==5) {duty=4; P0=disp_seg7[duty];}
else if(function==6)  {duty=5; P0=disp_seg7[duty];}
else if(function==7) {duty=6; P0=disp_seg7[duty];}
else if(function==8) {duty=7; P0=disp_seg7[duty];}
else if(function==9) {duty=8; P0=disp_seg7[duty];}
else if(function==10) {duty=9; P0=disp_seg7[duty];}
else if(function==11) {duty=10;P0=disp_seg7[duty];}
}
if(temp==2)
{
change_flag=~change_flag;
if(change_flag) { P0=disp_seg7[10]; sw_dly=5;}
else{ P0=disp_seg7[11]; sw_dly=30; }
if(rpt_count>50) { runpwm_flag=1;}
if(runpwm_flag) TR0=1;
}
}
}
void T0_INT() interrupt 1
{
if(change_flag)
{
TH0=(65536-200)/256;
TL0=(65536-200)%256;
}
else
{
TH0=(65536-400)/256;
TL0=(65536-400)%256;
}
timer++;
if(led_flag)
{
duty_count++;
if(duty_count==duty)
{
if(duty<10)
{
led_flag=0;
LED=OFF;
if(runpwm_flag){P0=0xff;}
}
}
}
if(timer%10==0)
{
led_flag=1;
duty_count=0;
if(runpwm_flag)
{
if(sw_dly==5) { P0=disp_seg7[11]; }//214.3hz
else if(sw_dly==30) { P0=disp_seg7[12]; }//401hz
}
else{ LED=ON; }
}
}
void T1_INT() interrupt 3
{
TH1=(65536-40000)/256;//214.695hz
TL1=(65536-40000)%256;
pwm_count++;
if(pwm_count%10==0 && runpwm_flag)
{
duty++;
if(duty>10)
duty=0;
}
}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...