MCS51 Countdown Timer 99นาที

Started by JENG, October 15, 2014, 09:33:21 PM

Previous topic - Next topic

JENG


โปรเจคนี้นานแล้วคับจำไม่ได้ว่าเคยลงไว้หรือยัง เอาเป็นว่าเอามาให้เพื่อนๆได้ลองทำเล่นแล้วกัน
ตอนนี้ MCS51 เริ่มจางๆไปเยอะ เพราะ MCU รุ่นใหม่ๆมีความสามารถมากขึ้นและราคาก็ถูกลงเรื่อยๆ

จำได้ว่าเคยทำเป็นโครงงานของเด็ก ม.ปลาย หรือ ปวช นี่แหละสองปีที่แล้วโน่น

code เป้นแบบ state machine ไม่มีการใช้ interrupt ใดๆทั้งสิ้น เพราะโจทย์ไม่ได้ซับซ้อนเป็น
งานง่ายๆ รับค่า switch สั่ง แสดงผลโดยการ scan ซึ่งงานนี้ใช้ MCS51 อย่าง AT89C2051 เหลือๆ

switch มีสามปุ่ม
1.Set
2.Up และ Start
3.Down และ Stop

การทำงานหัวใจหลักๆก็ AT89C2051 นี่แหละคับตัวเดียวเลย ;D

งานหยาบๆนิดนึงแต่มองง่ายประกอบง่าย








วิดีโอตอนทดสอบ

http://www.youtube.com/v/JYf3wl9bEAA


file pcb คับ eagle 5.6.0

https://drive.google.com/file/d/0B77_GeSXdC6rX24zdmY4Q1FycFE/view?usp=sharing



code ไม่สั้นไม่ยาวเขียนไว้นานแล้วอาจยังไม่เนี๊ยบไม่เนียนไม่ถูกหลักไม่ได้กรองไม่ได้สมบูรณ์แบบ
ตอนนั้นเพิ่งเข้าวงการกำลังหัดเดินด้วยภาษา C แต่ก็พอถูๆไถๆไปได้ ;D

/*
Program        :CountDown 4Digit
Date              :2/02/2012
Author           :Mr.Somlak Mangnimit
Compiler        :Keil 4.5
Device           :AT89C2051 @12MHz
*/


#include <at892051.h>


#define SegmentPort P1
#define SwitchSET P3_1
#define SwitchINC P3_0
#define SwitchDEC P3_2
#define Speaker P3_3


#define CommonDigit1 P1_0
#define CommonDigit2 P3_7
#define CommonDigit3 P3_5
#define CommonDigit4 P3_4


volatile bit
BlinkFlag,Alarm;


volatile unsigned char
DigitSEL,Debounce,SwitchSTATE,
BlinkPOS,BlinkCNT,
CounterMODE,CounterVAL,
SEC,MIN,Num1,Num2,Num3,Num4,
AlarmCNT
;


unsigned char code SegTAB[] = {
//-0----1----2----3----4----5----6----7----8----9----[----]----t--neg-blank
//0x80,0xf3,0x49,0x61,0x33,0x25,0x05,0xf1,0x01,0x21,0x8d,0xe1,0x0f,0x7f,0xff
0x81,0xe7,0x49,0x45,0x27,0x15,0x11,0xc7,0x01,0x05,0x39,0xff
};


//===============//
// Wait tick 5ms //
//===============//
void WaitTick(void){
while(TF0==0);
TF0 = 0;
TH0 = 0xec;
TL0 = 0x78;
BlinkCNT++;
if(BlinkCNT==50){
BlinkFlag = ~BlinkFlag;
BlinkCNT = 0x00;
}
}


//================//
// scan 7 segment //
//================//
void Scan(void){
WaitTick();
CommonDigit1 = 1;
CommonDigit2 = 1;
CommonDigit3 = 1;
CommonDigit4 = 1;
DigitSEL++;
if(BlinkPOS==5&&BlinkFlag==1){return;}


switch(DigitSEL){
case 1:
SegmentPort = SegTAB[Num1]|1;
if(BlinkPOS==1){
CommonDigit1 = BlinkFlag;
break;
}
CommonDigit1 = 0;
break;


case 2:
SegmentPort = SegTAB[Num2]|1;
if(BlinkPOS==2){
CommonDigit2 = BlinkFlag;
break;
}
CommonDigit2 = 0;
break;


case 3:
SegmentPort = SegTAB[Num3]|1;
if(BlinkPOS==3){
CommonDigit3 = BlinkFlag;
break;
}
CommonDigit3 = 0;
break;


default:
DigitSEL = 0x00;
SegmentPort = SegTAB[Num4]|1;
if(BlinkPOS==4){
CommonDigit4 = BlinkFlag;
break;
}
CommonDigit4 = 0;
break;
}
}


//=================//
// Sound Generator //
//=================//
void BeepDelay(int val){
while(--val);
}


void SoundDelay(int val){
unsigned long x = val*100;
while(--x);
}


void Beep(void){
int Duration;
for(Duration=0;Duration<60;Duration++){
Speaker = 0;
BeepDelay(40);
Speaker = 1;
BeepDelay(40);
}
Speaker = 1;
}


void Sound(void){
unsigned char x;
for(x=0;x<4;x++){
Beep();
SoundDelay(20);
}
}


//=========//
// Initial //
//=========//
void Setup(void){
P1 = 0xff;
P3 = 0xff;
BlinkPOS = 0;
SwitchSTATE = 0;
Num1 = 0;
Num2 = 0;
Num3 = 0;
Num4 = 0;


TMOD = 0x21;
TR0 = 0;
TH0 = 0x78;
TL0 = 0xec;
TR0 = 1;
TF0 = 0;
}


//==============//
// main program //
//==============//
void main(void){
Setup();


while(1){
Scan();
Debounce++;


//-------------------------------------------------------------
// SwitchSTATE 0
//-------------------------------------------------------------
if(!SwitchSTATE&&(!SwitchSET||!SwitchINC||!SwitchDEC)){
SwitchSTATE = 1;
Debounce = 0;
}


//-------------------------------------------------------------
// SwitchSTATE 1
//-------------------------------------------------------------
if(SwitchSTATE==1&&Debounce==10){


//-------------------------------------------------
// SwitchSET
//-------------------------------------------------
if(!SwitchSET){
BlinkPOS++;
if(BlinkPOS<3){
BlinkPOS = 3;
Num3 = Num1;
Num4 = Num2;
Num1 = 11;
Num2 = 10;
}


if(BlinkPOS>4){
BlinkPOS = 0;
Num1 = Num3;
Num2 = Num4;
Num3 = 0;
Num4 = 0;
MIN = Num1<<4|Num2;
SEC = 0x60;
CounterVAL = 0;
}


CounterMODE = 0;
SwitchSTATE = 2;
continue;
}


//-------------------------------------------------
// SwitchINC Mode 0
//-------------------------------------------------
if(!SwitchINC&&!CounterMODE){
switch(BlinkPOS){
case 0:
if(!MIN&&SEC==0x60)break;
CounterMODE = 1; //Start
break;


case 3:
Num3++;
if(Num3>9){Num3=0;}
break;


case 4:
Num4++;
if(Num4>9){Num4=0;}
break;
}
SwitchSTATE = 2;
continue;
}


//-------------------------------------------------
// SwitchDEC Mode 0
//-------------------------------------------------
if(!SwitchDEC&&!CounterMODE){
switch(BlinkPOS){


case 3:
Num3--;
if(Num3==255){Num3=9;}
break;


case 4:
Num4--;
if(Num4==255){Num4=9;}
break;
}
SwitchSTATE = 2;
continue;
}


//-------------------------------------------------
// SwitchDEC Mode 1
//-------------------------------------------------
if(!SwitchDEC&&CounterMODE){
CounterMODE = 0;
}
}


//-------------------------------------------------------------
// SwitchSTATE 2
//-------------------------------------------------------------
if(SwitchSET&&SwitchINC&&SwitchDEC){
SwitchSTATE = 0;
}


//-------------------------------------------------------------
// Timer
//-------------------------------------------------------------
if(CounterMODE){
CounterVAL++;
if(CounterVAL==200){
CounterVAL = 0;
SEC--;
if((SEC&0x0f)==0x0f)SEC-=6;
if(!SEC&&!MIN){
Alarm = 1;
AlarmCNT = 0;
CounterMODE = 0;
}


if(!SEC){
SEC = 0x60;
Num3 = 0;
Num4 = 0;
continue;
}


if(MIN){
if(SEC==0x59){
MIN--;
if((MIN&0x0f)==0x0f)MIN-=6;
}
}


Num1 = MIN>>4;
Num2 = MIN&0x0f;
Num3 = SEC>>4;
Num4 = SEC&0x0f;
}
}


//-------------------------------------------------------------
// Timer
//-------------------------------------------------------------
if(Alarm&&!CounterMODE){
AlarmCNT++;
if(AlarmCNT==100){
AlarmCNT = 0;
CommonDigit1 = 1;
CommonDigit2 = 1;
CommonDigit3 = 1;
CommonDigit4 = 1;
Sound();
if(!SwitchSET||!SwitchINC||!SwitchDEC){
Alarm = 0;
AlarmCNT = 0;
}
}
}
}
}


ถ้าชอบใจก็ช่วยกันกด like ใต้รูป avata ของผม เพื่อเป็นกำลังใจกันเยอะๆนะคับ จะทำโปรเจคง่ายๆมาฝากกันเรื่อยๆ :-*

สามารถติดตาม electoday ได้ที่

Facebook
www.facebook.com/groups/coopmicro

Google+
https://plus.google.com/communities/103482067769375459277

☺☺☺ความรู้ และความฉลาด ไม่ใช่สิ่งเดียวกัน จะมีประโยชน์อะไร ถ้าฉลาดแต่อยู่ในกะลา☺☺☺

tape_4

มาสอนหน่อย state machine อยากรู้ว่ามันเป็นอย่างไร ได้โปรด ::)
จงเชื่อมั่นในสิ่งที่ทำและมีศรัทธากับสิ่งที่เราเรียนรู้

JENG

Quote from: tape_4 on October 15, 2014, 09:36:36 PM
มาสอนหน่อย state machine อยากรู้ว่ามันเป็นอย่างไร ได้โปรด ::)

ปิดรัชดาเลี้ยงสองคืนติ เดี๋ยวสอนให้หมดเปลือก ::)
สามารถติดตาม electoday ได้ที่

Facebook
www.facebook.com/groups/coopmicro

Google+
https://plus.google.com/communities/103482067769375459277

☺☺☺ความรู้ และความฉลาด ไม่ใช่สิ่งเดียวกัน จะมีประโยชน์อะไร ถ้าฉลาดแต่อยู่ในกะลา☺☺☺


S.poolpong93


Admin

โปรเจคน่าสนใจดีครับ
รับทำ PCB (ออกใบกำกับภาษีได้) => https://www.electoday.com

pntclub


Katoon

เจ๋งดีครับ ชอบๆ ขอบคุณที่ให้ความรู้ครับ