قسم الميكروكنترولر والروبوت ودوائر الاتصال بالحاسب الالي قسم المتحكمات الـ microcontroller و المعالجات microprocessor و التحكم الرقمي بالكمبيوتر CNC والانظمة الآلية والروبوت Robots

أدوات الموضوع

الصورة الرمزية المخترع01
المخترع01
:: مهندس متميز ::
تاريخ التسجيل: Dec 2009
المشاركات: 397
نشاط [ المخترع01 ]
قوة السمعة:0
قديم 28-03-2014, 08:22 PM المشاركة 1   
افتراضي تعديل كود زيادة عداد الوقت Twitter FaceBook Google+



السلام عليكم
عندي مشروع عداد السرعة و حساب المسفات
اريد زيادة عرض الوقت و اشهر و السنة
و عرضها عللى السيفن سيغمون
باستعمال العداد ds1307

atmega16
Atmel Studio البرنامج مكتوب ب
هل من مساعدة لزيادة الكود المهتم ب الوقت و عرضه
-----------------------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <avr/eeprom.h>


#define CPU_12

#ifdef CPU_12 //For 12MHz Crystal.
#define timDef 732
#define debLim 120
#define F_CPU 12000000
#else // for internal 8MHz oscillator
#define timDef 500
#define F_CPU 8000000
#define debLim 85
#endif

#include <util/delay.h>
#define DISPPORT PORTC // Port for Display 7 Segments and Dot too.
#define DISPDDR DDRC // Direction.

#define sA 0
#define sB 1
#define sC 2
#define sD 3
#define sE 4
#define sF 5
#define sG 6
#define sT 7

#define DISPSEL PORTD
#define DISPSELDDR DDRD


#define DISP4 0
#define DISP3 1
#define DISP2 2
#define DISP1 3
#define DISP0 4

#define LEDPORT PORTB
#define LEDPORTDDR DDRB
#define LEDPIN PINB


#define LED3 7
#define LED2 4
#define LED1 5
#define ISIG 2
#define SW4 0

#define SWPORT PORTA
#define SWDDR DDRA
#define SWPIN PINA

#define PULSEMAX 2

#define SW1 4
#define SW2 5
#define SW3 7


#define setbit(port, bit) (port) |= (1 << (bit))
#define clearbit(port, bit) (port) &= ~(1 << (bit))
void dispall(void);
void programCodeMeter(void);
void eepromWriteTrip(void);
void eepromSettingRead(void);
void disp(char num, char dig, char dot);
void init_ports(void);
void init_tmr0(void);
void init_int0(void);
void hireMeter(void);
void tripMeter(void);
void programMeter(void);
void hiredContMeter(void);
void stopMeter(void);
void waitMeter(void);
void checkSwitches(void);
void startupDelay(void);
void ftotKm(void);
void ftotKm2(void);

volatile unsigned long int MAINDISP, MAINDOTS;
enum STATES {stopped, hired, wait, trip, program, programCode, hiredCont, pinitDrop, prsPerKm, ppulsePerKm, psHORT, ptimedUnit, ptotKm, ptotKm2 } meterStates;
enum SWITCHSTATE {release, press} switch1, switch2, switch3, switch4, debouncer;
unsigned char pulseFilt;
unsigned long int pulseACC;
unsigned int initialDrop, sHORT, rsPerKm;
unsigned int pulsePerKm, pulsePerShort, timedShort, timePerShort;
unsigned int timedPrice, initialWait, currWait, currPulses, tripDisplay, MOFFSET;
unsigned int totKm, totKm2, kmCount;
unsigned char firstKMcnt;

uint16_t eeTrips ;
uint16_t eetotKm;
uint16_t eeKM ;
uint16_t eeTIMED ;
uint16_t eeINIT ;
uint16_t eeSHORT;
uint16_t eePPKM;
uint16_t PulseIncShort;
uint16_t TimeIncShort;
uint16_t eepulseACC, eetimedShort, eecurrWait, eeMAINDISP;
uint8_t eeMState;

ISR(INT0_vect)
{
if (pulseFilt == PULSEMAX) { // see if the pulse is out of the pulse filter then assume it valid
pulseFilt = 0;

if (currPulses < pulsePerKm)
currPulses++;
else {
if ( firstKMcnt == 0 ) {
firstKMcnt = 1;
totKm++;
totKm2++;
}

pulseACC++; //accumulate the pulses.
currWait = initialWait;
}

if ((MAINDOTS % 10) == 1)
MAINDOTS--;
else
MAINDOTS++; //toggle the main dot.
}

}

ISR(TIMER0_OVF_vect) //Timer0 overflowed.
{
static unsigned char debCnt;
static unsigned int timCnt;

//**********************************************
if ( debouncer == release ) //debouncing is done, check the switches
{ //only checks 1 switch per time. if two or more are pressed, sw1 has higher priority then.
if ( !(SWPIN & (1<<SW1))) {
switch1 = press;
debouncer = press;
//MAINDISP++;
}
else if ( !(SWPIN & (1<<SW2))) {
switch2 = press;
debouncer = press;
//MAINDISP+=2;
}
else if (!(SWPIN & (1<<SW3))) {
switch3 = press;
debouncer = press;
//MAINDISP+=3;
}
else if (!(LEDPIN & (1<<SW4))) {
switch4 = press;
debouncer = press;
//MAINDISP+=4;
}
}
//*********************************************
if (debouncer == press) {
debCnt++;
if (debCnt >= debLim) {
debCnt = 0;
debouncer = release;
}
}
//*********************************************
dispall();
//*********************************************
if (pulseFilt >= PULSEMAX)
pulseFilt = PULSEMAX;
else
pulseFilt++;
//*********************************************

timCnt++;
if (timCnt >= timDef * 6 ) { //6 seconds delay.
timCnt =0;
if ( currWait < initialWait )
currWait++;
else {
timedShort++;
currPulses = pulsePerKm;
}
}

}

void eepromWriteTrip(void)
{
cli();
eeprom_write_word(&eeTrips, tripDisplay);
sei();
}

void write_eepromSettings(void)
{
cli();
eeprom_write_word(&eeTrips, tripDisplay);
eeprom_write_word(&eeKM, rsPerKm);
eeprom_write_word(&eeTIMED, timedPrice);
eeprom_write_word(&eeINIT, initialDrop);
eeprom_write_word(&eeSHORT, sHORT);
eeprom_write_word(&eePPKM, pulsePerKm);
sei();
pulsePerShort = (pulsePerKm) / (rsPerKm / sHORT);
timePerShort = (uint16_t) (600 * sHORT / timedPrice);
initialWait = initialDrop * 600 / timedPrice;
}

void eepromStateRead(void)
{
cli();
meterStates = eeprom_read_byte(&eeMState);
pulseACC = eeprom_read_word(&eepulseACC);
timedShort = eeprom_read_word(&eetimedShort);
currWait = eeprom_read_word(&eecurrWait);
MAINDISP = eeprom_read_word(&eeMAINDISP);
totKm = eeprom_read_word(&eetotKm);

if ( totKm > 9999 )
totKm = 0;

if ( pulseACC > 0 )
currPulses = pulsePerKm; //if pulses were accumulated
else
currPulses = 0;

if ( meterStates != hired && meterStates != hiredCont && meterStates != wait ) {
meterStates = stopped;
pulseACC = 0;
timedShort = 0;
currWait = 0;
currPulses = 0;
}

kmCount = 0;
totKm2 = 0;

sei();

switch (meterStates)
{
case wait:
MAINDOTS = 10;
setbit(LEDPORT, LED3);
setbit(LEDPORT, LED2);
clearbit(LEDPORT, LED1);
break;
case hired:
MAINDOTS = 10;
hireMeter();
meterStates = wait;
break;
case hiredCont:
MAINDOTS = 10;
clearbit(LEDPORT, LED3);
setbit(LEDPORT, LED2);
clearbit(LEDPORT, LED1);
break;
default:
meterStates = stopped;
}
}

void eepromStateWrite(void)
{
cli();
eeprom_write_byte(&eeMState, meterStates);
eeprom_write_word(&eepulseACC, pulseACC);
eeprom_write_word(&eetimedShort, timedShort);
eeprom_write_word(&eecurrWait, currWait);
eeprom_write_word(&eeMAINDISP, MAINDISP);
eeprom_write_word(&eetotKm, totKm);
sei();
}

void eepromSettingRead(void)
{
cli();
tripDisplay = eeprom_read_word(&eeTrips);
if ( tripDisplay > 9999 )
tripDisplay = 0;

rsPerKm = eeprom_read_word(&eeKM);

if ( rsPerKm > 999 )
rsPerKm = 100;

timedPrice= eeprom_read_word(&eeTIMED);

if ( timedPrice > 999 )
timedPrice = 22;

initialDrop = eeprom_read_word(&eeINIT);

if ( initialDrop > 999 )
initialDrop = 963;

sHORT = eeprom_read_word(&eeSHORT);

if ( sHORT >= rsPerKm )
sHORT = 99;

pulsePerKm = eeprom_read_word(&eePPKM);

if ( pulsePerKm > 9999 )
pulsePerKm = 640;


pulsePerShort = (pulsePerKm) / (rsPerKm / sHORT);
timePerShort = (uint16_t) (600 * sHORT / timedPrice);
initialWait = initialDrop * 600/ timedPrice;
sei();

MAINDISP = 0; // to be changed depending on last meter state.
MAINDOTS = 0;
}

void init_int0(void)
{

MCUCR = (1<<ISC01) | (1<<ISC00); //rising edge will trigger interrupt.
GICR |= (1<<INT0);
sei();
}

void dispall(void) //displays whatever is in MAINDISP
//dots are in MAINDOTS
{
static char dispIndex;
static long int MAINDISPTEMP;
static long int MAINDOTSTEMP;

unsigned char temp,tdot;

switch (dispIndex)
{

case 0:
temp = MAINDISPTEMP % 10;
tdot = MAINDOTSTEMP % 10;
if ( MAINDISPTEMP == 0)
temp = 12;
disp(temp, dispIndex, tdot);
break;
case 1:
temp = ((MAINDISPTEMP % 100) / 10);
tdot = ((MAINDOTSTEMP % 100) / 10);
if ( MAINDISPTEMP < 10)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 2:
temp = ((MAINDISPTEMP %1000)/ 100);
tdot = ((MAINDOTSTEMP %1000)/ 100);
if ( MAINDISPTEMP < 100)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 3:
temp = ((MAINDISPTEMP % 10000) / 1000);
tdot = ((MAINDOTSTEMP % 10000) / 1000);
if ( MAINDISPTEMP < 1000)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 4:
temp = ((MAINDISPTEMP % 100000) / 10000);
tdot = ((MAINDOTSTEMP % 100000) / 10000);
if ( MAINDISPTEMP < 10000)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 5:
//temp = (MAINDISPTEMP / 10000);
//tdot = (MAINDOTSTEMP / 10000);
//if ( MAINDISPTEMP < 10000)
// temp = 12; //will display nothing
//disp(temp, dispIndex, tdot);
DISPSEL = (1 << 7);
break;
//case 5:
// DISPSEL = (1 << 7);
break;

}

dispIndex++; //next time the index will be the next digit.
if ( dispIndex > 5) {
dispIndex = 0;
MAINDISPTEMP = MAINDISP;
MAINDOTSTEMP = MAINDOTS;
}
}

void disp(char num, char dig, char dot)
{
switch( num ) {
case 0:
DISPPORT = 0b00111111;
break;
case 1:
DISPPORT = 0b00000110;
break;
case 2:
DISPPORT = 0b01011011;
break;
case 3:
DISPPORT = 0b01001111;
break;
case 4:
DISPPORT = 0b01100110;
break;
case 5:
DISPPORT = 0b01101101;
break;
case 6:
DISPPORT = 0b01111101;
break;
case 7:
DISPPORT = 0b00000111;
break;
case 8:
DISPPORT = 0b01111111;
break;
case 9:
DISPPORT = 0b01100111;
break;
default:
DISPPORT = 0b00000000;
}

switch (dig)
{
case 0:
DISPSEL = (1<<DISP0);
break;
case 1:
DISPSEL = (1<<DISP1);
break;
case 2:
DISPSEL = (1<<DISP2);
break;
case 3:
DISPSEL = (1<<DISP3);
break;
case 4:
DISPSEL = (1<<DISP4);
break;
default:
DISPSEL = (1<<DISP0);
}

DISPSEL |= (1<<7);
if ( dot == 1 )
DISPPORT |= 0x80; //turn on the DOT
}


void init_ports(void)
{
DISPPORT = 0xFF;
DISPDDR = 0xFF;

DISPSEL = ((0<<DISP4) | (0<<DISP3) | (0<<DISP2) | (0<<DISP1) | (0<<DISP0)) |(1<<7);
DISPSELDDR = ((1<<DISP4) | (1<<DISP3) | (1<<DISP2) | (1<<DISP1) | (1<<DISP0)) | (1<<7);

LEDPORT = ((0<<LED3) | (0<<LED2) | (1<<LED1) | (0<<ISIG) | (1<<SW4));
LEDPORTDDR = ((1<<LED3) | (1<<LED2) | (1<<LED1) | (0<<ISIG) | (0<<SW4));



SWPORT = 0xFF; //all switches are pulled low.
SWDDR = ((0<<SW1) | (0<<SW2) | (0<<SW3));

PORTD= 0xFF;
DDRD= 0xFF;
}


void init_tmr0(void)
{
TCCR0 = 0b00000011; //Prescalar = 1024. delay = 1.28 msec
TIMSK |= (1<<TOIE0);//Enable Timer0 Overflow Interrupt.
sei(); //Enable global interrupts.

}

void ftotKm(void)
{
MAINDISP = totKm;
setbit(LEDPORT, LED2);
}

void ftotKm2(void)
{
MAINDISP = totKm2;
clearbit(LEDPORT, LED2);
}

void checkSwitches(void)
{
switch (meterStates) {
case stopped:
if ( switch2 == press ) {
meterStates = hired;
hireMeter();
}
else if (switch3 == press) {
meterStates = trip;
tripMeter();
}
else if (switch4 == press) {
meterStates = programCode;
programCodeMeter();
}
break;
case programCode:
if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP -= MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP += MOFFSET;
//else
//MAINDISP = 9999; else remain the display as is.
}
else if ( switch1 == press) {
if (MAINDISP == 23) {
meterStates = program;
programMeter();
}
else {
meterStates = stopped;
stopMeter();
}
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case pinitDrop:
if ( switch2 == press ) {
if ((MAINDISP + MOFFSET) < 9999)
MAINDISP+= MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET)> 0 )
MAINDISP-= MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
initialDrop = MAINDISP;
MAINDISP = rsPerKm;
meterStates = prsPerKm;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case prsPerKm:
if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP+= MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
rsPerKm = MAINDISP;
meterStates = ppulsePerKm;
MAINDISP = pulsePerKm;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case ppulsePerKm:
if ( switch2 == press ) {
if ((MAINDISP + MOFFSET)< 9999)
MAINDISP+= MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
pulsePerKm = MAINDISP;
meterStates = psHORT;
MAINDISP = sHORT;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case psHORT:
if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP+=MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
sHORT = MAINDISP;
meterStates = ptimedUnit;
MAINDISP = timedPrice;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case ptimedUnit:
if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP+=MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
timedPrice = MAINDISP;
write_eepromSettings();
meterStates = stopped;
stopMeter();
sei();
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case hiredCont:
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
else if (switch3 == press) {
meterStates = wait;
waitMeter();
}
break;
case wait:
if (switch3 == press) {
meterStates = hiredCont;
hiredContMeter();
}
else if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
break;
case trip:
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
else if (switch2 == press) {
meterStates = ptotKm;
ftotKm();
}
break;
case ptotKm:
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
else if (switch2 == press) {
meterStates = ptotKm2;
ftotKm2();
}
break;

case ptotKm2:
case program:
case hired: //this state is only a transition state when hire button is pressed.
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
break;
}
switch1 = release; //release all switches.
switch2 = release;
switch3 = release;
switch4 = release;
}

void hireMeter(void)
{ //hire the meter initially.
pulseACC = 0;
currWait = 0;
currPulses = 0;
timedShort = 0;
eepromSettingRead(); //Read the overall settings

tripDisplay++; //add another trip now.


MAINDISP = initialDrop;
MAINDOTS = 10; //Light the Paisa Dot.

eepromWriteTrip();
eepromStateWrite();

totKm2 = 0;
firstKMcnt = 0;

setbit(LEDPORT, LED2); //set Meter Hire Bit
clearbit(LEDPORT, LED1); //clear Meter Available bit
setbit(LEDPORT, LED3); //set Meter Waiting Bit
}

void programCodeMeter(void)
{

MAINDISP = 50;
MAINDOTS = 0;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
MOFFSET = 1;
break;
}
}

void waitMeter(void)
{
timedShort = 0;
eepromStateWrite();
setbit(LEDPORT, LED3);
setbit(LEDPORT, LED2);
clearbit(LEDPORT, LED1);
}

void hiredContMeter(void)
{
eepromStateWrite();
clearbit(LEDPORT, LED3);
}

void tripMeter(void)
{
MAINDISP = tripDisplay;
MAINDOTS = 1;
clearbit(LEDPORT, LED1);
clearbit(LEDPORT, LED2);
setbit(LEDPORT, LED3);
}

void programMeter(void)
{
eepromSettingRead();

// setbit(LEDPORT, LED1);
// setbit(LEDPORT, LED2);
// setbit(LEDPORT, LED3);

MAINDOTS = 1010;
MAINDISP = initialDrop;
meterStates = pinitDrop;
}

void stopMeter(void)
{
MAINDISP = 0;
MAINDOTS = 0;
eepromStateWrite();
clearbit(LEDPORT, LED2);
clearbit(LEDPORT, LED3);
setbit(LEDPORT, LED1);
}

void runMeter(void)
{
switch(meterStates)
{
case hired:
MAINDISP = initialDrop;
meterStates = wait; //Waiting is enabled by default
pulseACC = 0;
timedShort = 0;
break;
case hiredCont:
if (pulseACC >= pulsePerShort) {
pulseACC -= pulsePerShort;
MAINDISP += sHORT;
kmCount+= pulsePerShort;
if (kmCount >= pulsePerKm) {
kmCount -= pulsePerKm;
totKm++; // the one which will remain as is.
totKm2++;// the one which will eventually reset.
}

timedShort = 0; //Reset Timed Short in case of Pulse
eepromStateWrite();
}
break;
case wait:
if (pulseACC >= pulsePerShort) {
pulseACC -= pulsePerShort;
MAINDISP += sHORT;
kmCount+= pulsePerShort;
if (kmCount >= pulsePerKm) {
kmCount -= pulsePerKm;
totKm++; // the one which will remain as is.
totKm2++;// the one which will eventually reset.
}
timedShort=0;
eepromStateWrite();
}
if (timedShort >= timePerShort) {
timedShort -= timePerShort;
MAINDISP += sHORT;
pulseACC = 0; // Reset pulse short in case of Time
eepromStateWrite();
}
break;
case stopped:
MAINDISP = 0;
MAINDOTS = 0;
break;

case trip:
case program:
case ptotKm:
case ptotKm2:
case programCode:
case pinitDrop:
case prsPerKm:
case psHORT:
case ppulsePerKm:
case ptimedUnit:
break;

default:
meterStates = stopped;
}
}

void startupDelay(void)
{
unsigned int i;

for ( i = 0; i <= 3000; i++)
_delay_ms(1);
}

int main(void)
{
MAINDISP = 11111;
MAINDOTS = 11111;
MOFFSET = 1;
// initialDrop = 90;
// sHORT = 10; //1 rs. short.
// pulsePerKm = 640;
// rsPerKm = 80; //8 Rs per kilometer.
// pulsePerShort = (pulsePerKm / rsPerKm) * sHORT;

// timedPrice = 600; //60 rs in 1 hour., 1 re/min.
// initialWait = timedPrice * initialDrop / 600; //deci-minutes for Initial Drop.
// timePerShort = timedPrice * sHORT / 600; //deci-minutes

init_ports();
startupDelay(); //call a 3 seconds startup delay to ensure proper startup.

init_tmr0();
init_int0();

eepromSettingRead();
eepromStateRead();

while (1)
{
checkSwitches(); //check the switches to change state of meter.
runMeter(); // run the meter based on current state.
}

return 0;
}
-------------------------------------------------------------

اعلانات

حمودي الحلو
:: مهندس متواجد ::
تاريخ التسجيل: Sep 2006
الدولة: العراق
المشاركات: 105
نشاط [ حمودي الحلو ]
قوة السمعة:0
قديم 29-03-2014, 07:53 PM المشاركة 2   
افتراضي


السلام عليكم
اولا البرنامج غير مفهوم حاول ان ترفقه مع البروتيوس ليتم معالجته
ثانيا : حاول زيادة قيمة الكرستالة لكي تعطيك المطلوب

اعلانات اضافية ( قم بتسجيل الدخول لاخفائها )
  

الصورة الرمزية سعيد قادر
سعيد قادر
:: عضو ذهبي ::
تاريخ التسجيل: Apr 2009
الدولة: العراق/ كوردستان
المشاركات: 2,916
نشاط [ سعيد قادر ]
قوة السمعة:152
قديم 29-03-2014, 09:24 PM المشاركة 3   
افتراضي


اعتقد هكذا افضل من حيث القراءة


كود:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <avr/eeprom.h>


#define CPU_12

#ifdef CPU_12 //For 12MHz Crystal.
#define timDef 732
#define debLim 120
#define F_CPU 12000000
#else // for internal 8MHz oscillator
#define timDef 500
#define F_CPU 8000000
#define debLim 85
#endif

#include <util/delay.h>
#define DISPPORT PORTC // Port for Display 7 Segments and Dot too.
#define DISPDDR DDRC // Direction.

#define sA 0
#define sB 1
#define sC 2
#define sD 3
#define sE 4
#define sF 5
#define sG 6
#define sT 7

#define DISPSEL PORTD
#define DISPSELDDR DDRD


#define DISP4 0
#define DISP3 1
#define DISP2 2
#define DISP1 3
#define DISP0 4

#define LEDPORT PORTB
#define LEDPORTDDR DDRB
#define LEDPIN PINB


#define LED3 7
#define LED2 4
#define LED1 5
#define ISIG 2
#define SW4 0

#define SWPORT PORTA
#define SWDDR DDRA
#define SWPIN PINA

#define PULSEMAX 2

#define SW1 4
#define SW2 5
#define SW3 7


#define setbit(port, bit) (port) |= (1 << (bit))
#define clearbit(port, bit) (port) &= ~(1 << (bit))
void dispall(void);
void programCodeMeter(void);
void eepromWriteTrip(void);
void eepromSettingRead(void);
void disp(char num, char dig, char dot);
void init_ports(void);
void init_tmr0(void);
void init_int0(void);
void hireMeter(void);
void tripMeter(void);
void programMeter(void);
void hiredContMeter(void);
void stopMeter(void);
void waitMeter(void);
void checkSwitches(void);
void startupDelay(void);
void ftotKm(void);
void ftotKm2(void);

volatile unsigned long int MAINDISP, MAINDOTS;
enum STATES {stopped, hired, wait, trip, program, programCode, hiredCont, pinitDrop, prsPerKm, ppulsePerKm, psHORT, ptimedUnit, ptotKm, ptotKm2 } meterStates;
enum SWITCHSTATE {release, press} switch1, switch2, switch3, switch4, debouncer;
unsigned char pulseFilt;
unsigned long int pulseACC;
unsigned int initialDrop, sHORT, rsPerKm;
unsigned int pulsePerKm, pulsePerShort, timedShort, timePerShort;
unsigned int timedPrice, initialWait, currWait, currPulses, tripDisplay, MOFFSET;
unsigned int totKm, totKm2, kmCount;
unsigned char firstKMcnt;

uint16_t eeTrips ;
uint16_t eetotKm;
uint16_t eeKM ;
uint16_t eeTIMED ;
uint16_t eeINIT ;
uint16_t eeSHORT;
uint16_t eePPKM;
uint16_t PulseIncShort;
uint16_t TimeIncShort;
uint16_t eepulseACC, eetimedShort, eecurrWait, eeMAINDISP;
uint8_t eeMState;

ISR(INT0_vect)
{
if (pulseFilt == PULSEMAX) { // see if the pulse is out of the pulse filter then assume it valid
pulseFilt = 0;

if (currPulses < pulsePerKm)
currPulses++;
else {
if ( firstKMcnt == 0 ) {
firstKMcnt = 1;
totKm++;
totKm2++;
}

pulseACC++; //accumulate the pulses.
currWait = initialWait;
}

if ((MAINDOTS % 10) == 1)
MAINDOTS--;
else
MAINDOTS++; //toggle the main dot.
}

}

ISR(TIMER0_OVF_vect) //Timer0 overflowed.
{
static unsigned char debCnt;
static unsigned int timCnt;

//**********************************************
if ( debouncer == release ) //debouncing is done, check the switches
{ //only checks 1 switch per time. if two or more are pressed, sw1 has higher priority then.
if ( !(SWPIN & (1<<SW1))) {
switch1 = press;
debouncer = press;
//MAINDISP++;
}
else if ( !(SWPIN & (1<<SW2))) {
switch2 = press;
debouncer = press;
//MAINDISP+=2;
}
else if (!(SWPIN & (1<<SW3))) {
switch3 = press;
debouncer = press;
//MAINDISP+=3;
}
else if (!(LEDPIN & (1<<SW4))) {
switch4 = press;
debouncer = press;
//MAINDISP+=4;
}
}
//*********************************************
if (debouncer == press) {
debCnt++;
if (debCnt >= debLim) {
debCnt = 0;
debouncer = release;
}
}
//*********************************************
dispall();
//*********************************************
if (pulseFilt >= PULSEMAX)
pulseFilt = PULSEMAX;
else
pulseFilt++;
//*********************************************

timCnt++;
if (timCnt >= timDef * 6 ) { //6 seconds delay.
timCnt =0;
if ( currWait < initialWait )
currWait++;
else {
timedShort++;
currPulses = pulsePerKm;
}
}

}

void eepromWriteTrip(void)
{
cli();
eeprom_write_word(&eeTrips, tripDisplay);
sei();
}

void write_eepromSettings(void)
{
cli();
eeprom_write_word(&eeTrips, tripDisplay);
eeprom_write_word(&eeKM, rsPerKm);
eeprom_write_word(&eeTIMED, timedPrice);
eeprom_write_word(&eeINIT, initialDrop);
eeprom_write_word(&eeSHORT, sHORT);
eeprom_write_word(&eePPKM, pulsePerKm);
sei();
pulsePerShort = (pulsePerKm) / (rsPerKm / sHORT);
timePerShort = (uint16_t) (600 * sHORT / timedPrice);
initialWait = initialDrop * 600 / timedPrice;
}

void eepromStateRead(void)
{
cli();
meterStates = eeprom_read_byte(&eeMState);
pulseACC = eeprom_read_word(&eepulseACC);
timedShort = eeprom_read_word(&eetimedShort);
currWait = eeprom_read_word(&eecurrWait);
MAINDISP = eeprom_read_word(&eeMAINDISP);
totKm = eeprom_read_word(&eetotKm);

if ( totKm > 9999 )
totKm = 0;

if ( pulseACC > 0 )
currPulses = pulsePerKm; //if pulses were accumulated
else
currPulses = 0;

if ( meterStates != hired && meterStates != hiredCont && meterStates != wait ) {
meterStates = stopped;
pulseACC = 0;
timedShort = 0;
currWait = 0;
currPulses = 0;
}

kmCount = 0;
totKm2 = 0;

sei();

switch (meterStates)
{
case wait:
MAINDOTS = 10;
setbit(LEDPORT, LED3);
setbit(LEDPORT, LED2);
clearbit(LEDPORT, LED1);
break;
case hired:
MAINDOTS = 10;
hireMeter();
meterStates = wait;
break;
case hiredCont:
MAINDOTS = 10;
clearbit(LEDPORT, LED3);
setbit(LEDPORT, LED2);
clearbit(LEDPORT, LED1);
break;
default:
meterStates = stopped;
}
}

void eepromStateWrite(void)
{
cli();
eeprom_write_byte(&eeMState, meterStates);
eeprom_write_word(&eepulseACC, pulseACC);
eeprom_write_word(&eetimedShort, timedShort);
eeprom_write_word(&eecurrWait, currWait);
eeprom_write_word(&eeMAINDISP, MAINDISP);
eeprom_write_word(&eetotKm, totKm);
sei();
}

void eepromSettingRead(void)
{
cli();
tripDisplay = eeprom_read_word(&eeTrips);
if ( tripDisplay > 9999 )
tripDisplay = 0;

rsPerKm = eeprom_read_word(&eeKM);

if ( rsPerKm > 999 )
rsPerKm = 100;

timedPrice= eeprom_read_word(&eeTIMED);

if ( timedPrice > 999 )
timedPrice = 22;

initialDrop = eeprom_read_word(&eeINIT);

if ( initialDrop > 999 )
initialDrop = 963;

sHORT = eeprom_read_word(&eeSHORT);

if ( sHORT >= rsPerKm )
sHORT = 99;

pulsePerKm = eeprom_read_word(&eePPKM);

if ( pulsePerKm > 9999 )
pulsePerKm = 640;


pulsePerShort = (pulsePerKm) / (rsPerKm / sHORT);
timePerShort = (uint16_t) (600 * sHORT / timedPrice);
initialWait = initialDrop * 600/ timedPrice;
sei();

MAINDISP = 0; // to be changed depending on last meter state.
MAINDOTS = 0;
}

void init_int0(void)
{

MCUCR = (1<<ISC01) | (1<<ISC00); //rising edge will trigger interrupt.
GICR |= (1<<INT0);
sei();
}

void dispall(void) //displays whatever is in MAINDISP
//dots are in MAINDOTS
{
static char dispIndex;
static long int MAINDISPTEMP;
static long int MAINDOTSTEMP;

unsigned char temp,tdot;

switch (dispIndex)
{

case 0:
temp = MAINDISPTEMP % 10;
tdot = MAINDOTSTEMP % 10;
if ( MAINDISPTEMP == 0)
temp = 12;
disp(temp, dispIndex, tdot);
break;
case 1:
temp = ((MAINDISPTEMP % 100) / 10);
tdot = ((MAINDOTSTEMP % 100) / 10);
if ( MAINDISPTEMP < 10)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 2:
temp = ((MAINDISPTEMP %1000)/ 100);
tdot = ((MAINDOTSTEMP %1000)/ 100);
if ( MAINDISPTEMP < 100)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 3:
temp = ((MAINDISPTEMP % 10000) / 1000);
tdot = ((MAINDOTSTEMP % 10000) / 1000);
if ( MAINDISPTEMP < 1000)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 4:
temp = ((MAINDISPTEMP % 100000) / 10000);
tdot = ((MAINDOTSTEMP % 100000) / 10000);
if ( MAINDISPTEMP < 10000)
temp = 12; //will display nothing
disp(temp, dispIndex, tdot);
break;
case 5:
//temp = (MAINDISPTEMP / 10000);
//tdot = (MAINDOTSTEMP / 10000);
//if ( MAINDISPTEMP < 10000)
// temp = 12; //will display nothing
//disp(temp, dispIndex, tdot);
DISPSEL = (1 << 7);
break;
//case 5:
// DISPSEL = (1 << 7);
break;

}

dispIndex++; //next time the index will be the next digit.
if ( dispIndex > 5) {
dispIndex = 0;
MAINDISPTEMP = MAINDISP;
MAINDOTSTEMP = MAINDOTS;
}
}

void disp(char num, char dig, char dot)
{
switch( num ) {
case 0:
DISPPORT = 0b00111111;
break;
case 1:
DISPPORT = 0b00000110;
break;
case 2:
DISPPORT = 0b01011011;
break;
case 3:
DISPPORT = 0b01001111;
break;
case 4:
DISPPORT = 0b01100110;
break;
case 5:
DISPPORT = 0b01101101;
break;
case 6:
DISPPORT = 0b01111101;
break;
case 7:
DISPPORT = 0b00000111;
break;
case 8:
DISPPORT = 0b01111111;
break;
case 9:
DISPPORT = 0b01100111;
break;
default:
DISPPORT = 0b00000000;
}

switch (dig)
{
case 0:
DISPSEL = (1<<DISP0);
break;
case 1:
DISPSEL = (1<<DISP1);
break;
case 2:
DISPSEL = (1<<DISP2);
break;
case 3:
DISPSEL = (1<<DISP3);
break;
case 4:
DISPSEL = (1<<DISP4);
break;
default:
DISPSEL = (1<<DISP0);
}

DISPSEL |= (1<<7);
if ( dot == 1 )
DISPPORT |= 0x80; //turn on the DOT
}


void init_ports(void)
{
DISPPORT = 0xFF;
DISPDDR = 0xFF;

DISPSEL = ((0<<DISP4) | (0<<DISP3) | (0<<DISP2) | (0<<DISP1) | (0<<DISP0)) |(1<<7);
DISPSELDDR = ((1<<DISP4) | (1<<DISP3) | (1<<DISP2) | (1<<DISP1) | (1<<DISP0)) | (1<<7);

LEDPORT = ((0<<LED3) | (0<<LED2) | (1<<LED1) | (0<<ISIG) | (1<<SW4));
LEDPORTDDR = ((1<<LED3) | (1<<LED2) | (1<<LED1) | (0<<ISIG) | (0<<SW4));



SWPORT = 0xFF; //all switches are pulled low.
SWDDR = ((0<<SW1) | (0<<SW2) | (0<<SW3));

PORTD= 0xFF;
DDRD= 0xFF;
}


void init_tmr0(void)
{
TCCR0 = 0b00000011; //Prescalar = 1024. delay = 1.28 msec
TIMSK |= (1<<TOIE0);//Enable Timer0 Overflow Interrupt.
sei(); //Enable global interrupts.

}

void ftotKm(void)
{
MAINDISP = totKm;
setbit(LEDPORT, LED2);
}

void ftotKm2(void)
{
MAINDISP = totKm2;
clearbit(LEDPORT, LED2);
}

void checkSwitches(void)
{
switch (meterStates) {
case stopped:
if ( switch2 == press ) {
meterStates = hired;
hireMeter();
}
else if (switch3 == press) {
meterStates = trip;
tripMeter();
}
else if (switch4 == press) {
meterStates = programCode;
programCodeMeter();
}
break;
case programCode:
if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP -= MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP += MOFFSET;
//else
//MAINDISP = 9999; else remain the display as is.
}
else if ( switch1 == press) {
if (MAINDISP == 23) {
meterStates = program;
programMeter();
}
else {
meterStates = stopped;
stopMeter();
}
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case pinitDrop:
if ( switch2 == press ) {
if ((MAINDISP + MOFFSET) < 9999)
MAINDISP+= MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET)> 0 )
MAINDISP-= MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
initialDrop = MAINDISP;
MAINDISP = rsPerKm;
meterStates = prsPerKm;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case prsPerKm:
if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP+= MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
rsPerKm = MAINDISP;
meterStates = ppulsePerKm;
MAINDISP = pulsePerKm;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case ppulsePerKm:
if ( switch2 == press ) {
if ((MAINDISP + MOFFSET)< 9999)
MAINDISP+= MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
pulsePerKm = MAINDISP;
meterStates = psHORT;
MAINDISP = sHORT;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case psHORT:
if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP+=MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
sHORT = MAINDISP;
meterStates = ptimedUnit;
MAINDISP = timedPrice;
MAINDOTS = 1010;
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case ptimedUnit:
if ( switch2 == press ) {
if ((MAINDISP+MOFFSET) < 9999)
MAINDISP+=MOFFSET;
//else
// MAINDISP = 9999;
}
else if ( switch3 == press ) {
if ((MAINDISP-MOFFSET) > 0 )
MAINDISP-=MOFFSET;
else
MAINDISP = MOFFSET;
}
else if ( switch1 == press )
{
timedPrice = MAINDISP;
write_eepromSettings();
meterStates = stopped;
stopMeter();
sei();
}
else if ( switch4 == press) {
MOFFSET = MOFFSET * 10;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
break;
}
if (MOFFSET > 100)
MOFFSET = 1;
}
break;
case hiredCont:
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
else if (switch3 == press) {
meterStates = wait;
waitMeter();
}
break;
case wait:
if (switch3 == press) {
meterStates = hiredCont;
hiredContMeter();
}
else if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
break;
case trip:
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
else if (switch2 == press) {
meterStates = ptotKm;
ftotKm();
}
break;
case ptotKm:
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
else if (switch2 == press) {
meterStates = ptotKm2;
ftotKm2();
}
break;

case ptotKm2:
case program:
case hired: //this state is only a transition state when hire button is pressed.
if (switch1 == press) {
meterStates = stopped;
stopMeter();
}
break;
}
switch1 = release; //release all switches.
switch2 = release;
switch3 = release;
switch4 = release;
}

void hireMeter(void)
{ //hire the meter initially.
pulseACC = 0;
currWait = 0;
currPulses = 0;
timedShort = 0;
eepromSettingRead(); //Read the overall settings

tripDisplay++; //add another trip now.


MAINDISP = initialDrop;
MAINDOTS = 10; //Light the Paisa Dot.

eepromWriteTrip();
eepromStateWrite();

totKm2 = 0;
firstKMcnt = 0;

setbit(LEDPORT, LED2); //set Meter Hire Bit
clearbit(LEDPORT, LED1); //clear Meter Available bit
setbit(LEDPORT, LED3); //set Meter Waiting Bit
}

void programCodeMeter(void)
{

MAINDISP = 50;
MAINDOTS = 0;
switch (MOFFSET) {
case 10:
clearbit( LEDPORT, LED1);
setbit( LEDPORT, LED2);
clearbit( LEDPORT, LED3);
break;
case 100:
setbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
clearbit(LEDPORT, LED3);
break;
default:
clearbit( LEDPORT, LED1);
clearbit( LEDPORT, LED2);
setbit( LEDPORT, LED3);
MOFFSET = 1;
break;
}
}

void waitMeter(void)
{
timedShort = 0;
eepromStateWrite();
setbit(LEDPORT, LED3);
setbit(LEDPORT, LED2);
clearbit(LEDPORT, LED1);
}

void hiredContMeter(void)
{
eepromStateWrite();
clearbit(LEDPORT, LED3);
}

void tripMeter(void)
{
MAINDISP = tripDisplay;
MAINDOTS = 1;
clearbit(LEDPORT, LED1);
clearbit(LEDPORT, LED2);
setbit(LEDPORT, LED3);
}

void programMeter(void)
{
eepromSettingRead();

// setbit(LEDPORT, LED1);
// setbit(LEDPORT, LED2);
// setbit(LEDPORT, LED3);

MAINDOTS = 1010;
MAINDISP = initialDrop;
meterStates = pinitDrop;
}

void stopMeter(void)
{
MAINDISP = 0;
MAINDOTS = 0;
eepromStateWrite();
clearbit(LEDPORT, LED2);
clearbit(LEDPORT, LED3);
setbit(LEDPORT, LED1);
}

void runMeter(void)
{
switch(meterStates)
{
case hired:
MAINDISP = initialDrop;
meterStates = wait; //Waiting is enabled by default
pulseACC = 0;
timedShort = 0;
break;
case hiredCont:
if (pulseACC >= pulsePerShort) {
pulseACC -= pulsePerShort;
MAINDISP += sHORT;
kmCount+= pulsePerShort;
if (kmCount >= pulsePerKm) {
kmCount -= pulsePerKm;
totKm++; // the one which will remain as is.
totKm2++;// the one which will eventually reset.
}

timedShort = 0; //Reset Timed Short in case of Pulse
eepromStateWrite();
}
break;
case wait:
if (pulseACC >= pulsePerShort) {
pulseACC -= pulsePerShort;
MAINDISP += sHORT;
kmCount+= pulsePerShort;
if (kmCount >= pulsePerKm) {
kmCount -= pulsePerKm;
totKm++; // the one which will remain as is.
totKm2++;// the one which will eventually reset.
}
timedShort=0;
eepromStateWrite();
}
if (timedShort >= timePerShort) {
timedShort -= timePerShort;
MAINDISP += sHORT;
pulseACC = 0; // Reset pulse short in case of Time
eepromStateWrite();
}
break;
case stopped:
MAINDISP = 0;
MAINDOTS = 0;
break;

case trip:
case program:
case ptotKm:
case ptotKm2:
case programCode:
case pinitDrop:
case prsPerKm:
case psHORT:
case ppulsePerKm:
case ptimedUnit:
break;

default:
meterStates = stopped;
}
}

void startupDelay(void)
{
unsigned int i;

for ( i = 0; i <= 3000; i++)
_delay_ms(1);
}

int main(void)
{
MAINDISP = 11111;
MAINDOTS = 11111;
MOFFSET = 1;
// initialDrop = 90;
// sHORT = 10; //1 rs. short.
// pulsePerKm = 640;
// rsPerKm = 80; //8 Rs per kilometer.
// pulsePerShort = (pulsePerKm / rsPerKm) * sHORT;

// timedPrice = 600; //60 rs in 1 hour., 1 re/min.
// initialWait = timedPrice * initialDrop / 600; //deci-minutes for Initial Drop.
// timePerShort = timedPrice * sHORT / 600; //deci-minutes

init_ports();
startupDelay(); //call a 3 seconds startup delay to ensure proper startup.

init_tmr0();
init_int0();

eepromSettingRead();
eepromStateRead();

while (1)
{
checkSwitches(); //check the switches to change state of meter.
runMeter(); // run the meter based on current state.
}

return 0;
}
-------------------------------------------------------------

إضافة رد

العلامات المرجعية

«     الموضوع السابق       الموضوع التالي    »
أدوات الموضوع

الانتقال السريع إلى


الساعة معتمدة بتوقيت جرينتش +3 الساعة الآن: 06:59 AM
موقع القرية الالكترونية غير مسؤول عن أي اتفاق تجاري أو تعاوني بين الأعضاء
فعلى كل شخص تحمل مسئولية نفسه إتجاه مايقوم به من بيع وشراء وإتفاق وأعطاء معلومات موقعه
التعليقات المنشورة لا تعبر عن رأي موقع القرية الالكترونية ولايتحمل الموقع أي مسؤولية قانونية حيال ذلك (ويتحمل كاتبها مسؤولية النشر)

Powered by vBulletin® Version 3.8.6, Copyright ©2000 - 2025