// Watch Winder Multi Deluxe //Import libraries #include #include #include #include #include #include #include // PIC18F26K22 configurations #pragma config FOSC=INTIO67,PLLCFG=ON,PRICLKEN=OFF,FCMEN=OFF,IESO=OFF,PWRTEN=ON #pragma config BOREN=OFF,BORV=190,WDTEN=OFF,WDTPS=512,PBADEN=OFF,MCLRE=INTMCLR #pragma config STVREN=OFF,LVP=OFF,DEBUG=OFF,CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF #pragma config CPB=OFF,CPD=OFF,WRT0=OFF,WRT1=OFF,WRT2=OFF,WRT3=OFF,WRTC=OFF #pragma config WRTB=OFF,WRTD=OFF,EBTR0=OFF,EBTR1=OFF,EBTR2=OFF,EBTR3=OFF,EBTRB=OFF #pragma config CCP3MX=PORTB5 // I/O pin definitions // RA0-RA7 = LCD #define ENK PORTBbits.RB0 // Rotation counter input #define UP PORTBbits.RB1 == 0 // Button up pressed #define DOWN PORTBbits.RB2 == 0 // Button down pressed #define START PORTBbits.RB3 == 0 // Button start pressed #define RIGHT PORTBbits.RB4 == 0 // Button right pressed #define LEFT PORTBbits.RB5 == 0 // Button left pressed // RB6-RB7 programming pins #define LCDRS LATCbits.LATC0 // LCD #define LCDE LATCbits.LATC1 // LCD //RC2 L293E PWM control //RC3 L293E current measurement #define IN2 LATCbits.LATC4 // L293E input #define IN1 LATCbits.LATC5 // L293E input // RC6-RC7 UART #define TimeOver INTCONbits.TMR0IF // Timer0 overload flag #define _XTAL_FREQ 8000000 // PIC oscillator frequency #define USE_AND_MASKS // Variable declarations unsigned char command,direction[15]; int pwm=0,hours[15],minutes[15],seconds[15],step=1,steps=1,position=1,cycle,NewEnc=0;; float TotalTime=0,speed[15],TotalStepTime[15],RotationTime=0,current=0; // Function declarations void PICinit(); void LCDinit(); void LCDsend(unsigned int command); void LCDwrite(unsigned int command); void LCDposition(unsigned int position ,unsigned int row); void LCDclear(); void Delay(unsigned long b); void Wait(long ms); void SetHours(); void SetMinutes (); void SetSeconds(); void SetDirection(); void SetSpeed(); void interrupt Encoder(); //****************************************************************************** // Motor current measurement int Current(int samples) // RC3 = AN15 { long j2,result=0; for(j2=0;j20;b--); } void Wait(long ms) { for(long bb=0;bb16 || row>2 || position==0 || row==0) LCDsend(128); else LCDsend((position+((row-1)*64))+127); } void LCDwrite(unsigned int command) { LCDRS=1; LCDsend(command); } void LCDsend(unsigned int command) { LATA=command; LCDE=1; Delay(5); LCDE=0; Delay(5); } void LCDclear() { LCDposition(1,1); printf(" "); LCDposition(1,2); printf(" "); LCDposition(1,1); } extern void putch(char c) // printf enable { LCDwrite(c); return; } //*********************************************************************************************************************************************************** //*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* //*********************************************************************************************************************************************************** void main() { PICinit();LCDinit(); // INITIAL WINDOW LCDposition(3,1); printf("WatchWinder"); LCDposition(2,2); printf("Multi Deluxe"); Wait(1000);LCDclear(); //****************************************************************************** // SETTINGS WINDOW (select number of steps) LCDposition(5,1);printf("SETTINGS"); LCDposition(1,2);printf("Steps: "); while(!START) { if(UP) steps++; if(DOWN) steps--; if(steps<1) steps=9; if(steps>9) steps=1; LCDposition(8,2);printf("%i" ,steps);Wait(150); LCDposition(8,2);printf(" " ,steps);Wait(100); } //****************************************************************************** // ROTATION PARAMETERS SETTING WINDOW (step time, direction, rotation speed rps) LCDclear(); LCDposition(1,1); printf("# Time : :"); LCDposition(1,2); printf("Dir: Rps:"); for(int a=0;a<=9;a++) {hours[a]=0; minutes[a]=0;seconds[a]=0;direction[a]='<';speed[a]=3.0;} for(step=1;step<=steps;step++) { LCDposition(2,1); printf("%i" ,step); LCDposition(9,1); printf("%02i" ,hours[step]); LCDposition(12,1); printf("%02i" ,minutes[step]); LCDposition(15,1); printf("%02i" ,seconds[step]); LCDposition(5,2); printf("%c" ,direction[step]); LCDposition(12,2); printf("%.1f" ,speed[step]); Wait(500); while(!START) { if(LEFT) {position--;if(position<1) position=5;Wait(200);} if(RIGHT){position++;if(position>5) position=1;Wait(200);} if(position==1) SetHours(); if(position==2) SetMinutes(); if(position==3) SetSeconds(); if(position==4) SetDirection(); if(position==5) SetSpeed(); } } for(int a=1;a<=9;a++) TotalStepTime[a]=(hours[a]*3600.0)+(minutes[a]*60.0)+seconds[a]; //****************************************************************************** // WATING FOR START WINDOW LCDclear(); Wait(500); LCDposition(3,1);printf("Press any key"); LCDposition(4,2);printf("for START!"); while(!START && !DOWN && !UP && !RIGHT && !LEFT); LCDclear(); //****************************************************************************** for(cycle=1;cycle<=steps;cycle++) { TotalTime=0; TimeOver=0; WRITETIMER0(0); // start timer if(direction[cycle]=='>') {IN1=1;IN2=0;} // set motor direction else {IN1=0;IN2=1;} pwm=50; CCPR1L=pwm; // set motor power while(TotalTime500) // turn off the motor if current over 500 mA is detected {IN1=0;IN2=0; LCDposition(1,2);printf(" Overcurrent !!!"); while(1); } hours[0]=(int)(TotalStepTime[0]/3600.0); minutes[0]=(int)(((unsigned long)TotalStepTime[0]%3600)/60.0); seconds[0]=((unsigned long)TotalStepTime[0]-(hours[0]*3600+minutes[0]*60)); speed[0]=1000000.0/RotationTime; if(speed[cycle]>speed[0]) {pwm++;if(pwm>255)pwm=255;} if(speed[cycle]24) hours[step]=0; LCDposition(9,1); printf(" "); Wait(100); LCDposition(9,1); printf("%02i" ,hours[step]); Wait(150); } } void SetMinutes () { while(!RIGHT && !LEFT && !START) { if(UP) minutes[step]++; if(DOWN) minutes[step]--; if(minutes[step]<0) minutes[step]=59; if(minutes[step]>59) minutes[step]=0; LCDposition(12,1); printf(" "); Wait(100); LCDposition(12,1); printf("%02i" ,minutes[step]); Wait(150); } } void SetSeconds() { while(!RIGHT && !LEFT && !START) { if(UP) seconds[step]++; if(DOWN) seconds[step]--; if(seconds[step]<0) seconds[step]=59; if(seconds[step]>59) seconds[step]=0; LCDposition(15,1); printf(" "); Wait(100); LCDposition(15,1); printf("%02i" ,seconds[step]); Wait(150); } } void SetDirection() { while(!RIGHT && !LEFT && !START) { if(DOWN) direction[step]='<'; if(UP) direction[step]='>'; LCDposition(5,2); printf(" "); Wait(100); LCDposition(5,2); printf("%c" ,direction[step]); Wait(150); } } void SetSpeed() { while(!RIGHT && !LEFT && !START) { if(UP) speed[step]=speed[step]+0.1; if(DOWN) speed[step]=speed[step]-0.1; if(speed[step]<1) speed[step]=10; if(speed[step]>10) speed[step]=1; LCDposition(12,2); printf(" " ,speed[step]); Wait(100); LCDposition(12,2); printf("%.1f" ,speed[step]); Wait(150); } } //****************************************************************************** //****************************************************************************** //****************************************************************************** // ROTATION COUNTER DETECTION void interrupt Encoder(void) { if(INTCONbits.INT0IF==1 && INTCONbits.INT0IE==1) { INTCONbits.INT0IE=0; INTCONbits.INT0IF=0; NewEnc=1; RotationTime=(READTIMER0()*32.0); TimeOver=0; WRITETIMER0(0); INTCONbits.INT0IE=1;INTCONbits.INT0IF=0; return; } }