Johns ugly code
jmurrayufo 05 May 2007 18:16
#include <pic.h> #include <stdio.h> #include "picserial.h" __CONFIG(INTIO & // Configuration bits WDTDIS & PWRTDIS & MCLRDIS & UNPROTECT & BORDIS & IESODIS & FCMDIS ); #define RED RA1 #define YEL RB4 #define ON 1 #define OFF 0 // // Function prototypes void init(void); void intpwm(void); void printstring(const char* s); void getstring(char buff[]); unsigned int myatoi(const char* s); void delayms(unsigned int ms); void setTMR1(int time); unsigned int stringlength(const char *s); void setPulseWidth(unsigned int pwm); void blinkled(int b); void feeder(const char* s); void error(void); char mygetch(void); void blankline(void); // Global Variables char err=0; //Global error flag, set to zero if error occurs void main(void){ //**********************************MAIN****************************** unsigned char buffer[5]; unsigned int pwm; init(); while(1){ printstring("Enter Number:"); getstring(buffer); //feeder(buffer); setPulseWidth(myatoi(buffer)); printf("\n"); }//End While(1) }//End main //**********************************MAIN****************************** void init(void){ int i; TRISA=0b00000000; TRISB=0b00000101; ANSEL = 0; // AN0 A/D, all others digital I/O ANSELH = 0; // Pin function digital I/O intpwm(); initserial(); YEL=ON; delayms(100); RED=ON; delayms(100); YEL=OFF; delayms(100); RED=OFF; } void printstring(const char* s){ int i; int length; length=stringlength(s); for (i=0;(i<length) && (s!=0);i++) { putch(*s); s++; } } void getstring(char buff[]){ int i,n; static int x=0,y=0; //Y holds the row, x holds the column int temp; char c=mygetch(); //test variable static char lines[10][5]; x=0; //set the left, right thingy to be zero temp = y; while((c!=0x0d) && (c!=0x0a) && (x<4)){ if ((c=='\b') && (x>0)) { //if c was 0x8 (a backspace) run the backspace putch(0x8); //Go back '\b' would also work. putch(' '); //Blank out that mother! putch(0x8); //Go back to where you wanted to go in the first place, but had to go to BANGOR before going there! ARGH! lines[y][x]=0; x--; } else if((c>='0') && (c<='9')) { //The above does the following. If "c" is between 0 and 9, or a ',' or a ';', accept the char. lines[y][x]=c; lines[y][x+1]=0; //Write the terminating zero to the next position. putch(c); x++; //Incriment i ONLY if a new char was eneterd } else if(((c==0xC1) || (c==0xC2)) && (y>0) ) { // up=>0xC1, down=>0xC2 if (c==0xC1) { //Up arrow y--; //Go back a row for (;x>0;x--) { putch(0x8); //Go back putch(' '); //Blank it out putch(0x8); //Return to where you started } for (i=0;lines[y][i]!=0;i++){ //For every none zero value, print the value. putch(lines[y][i]); lines[temp][i]=lines[y][i]; x++; } } //Close up else { //Down arrow if (y>9) { //If we moved down, and its the last row, cycle the values. for (i=0;i<9;i++){ for (n=0;n<5;n++) { lines[i][n]=lines[i+1][n]; //Set value equal to the value below it } } y=9; //Set y to the 10th position. for (n=0;n<5;n++) { //Blank that row lines[y][n]=0; x=0; //At begining of row, set to zero } for (;x>0;x--){ //Blank out the line putch(0x8); //Go back putch(' '); //Blank it out putch(0x8); //Return to where you started } } else { //End cycle, otherwise do this stuf... //This Area blanks out the line, then prints the 5 y++; for (;x>0;x--){ //Blank out the line putch(0x8); //Go back putch(' '); //Blank it out putch(0x8); //Return to where you started } for (i=0;lines[y][i]!=0;i++){ putch(lines[y][i]); x++; } } } //Close down } c=mygetch(); //Get a new char } for (i=0;i<5;i++) { //Feed to the buffer buff[i]=lines[y][i]; } printf("\n"); for (i=0;i<5;i++) { printf("%x ",buff[i]); } y = temp+1; //Next line for next time. if (y>9) { //If we moved down, and its the last row, cycle the values. for (i=0;i<9;i++){ for (n=0;n<5;n++) { lines[i][n]=lines[i+1][n]; //Set value equal to the value below it } } y=9; //Set y to the 10th position. for (n=0;n<5;n++) { //Blank that row lines[y][n]=0; } } printf("\nY=%x\n",y); } unsigned int myatoi(const char* s){ int length; int i; unsigned int result=0; length=stringlength(s); //blinkled(2); printf("%x\n",length); for (i=0;i<length;i++) { result=result*10; result+=s[i]-'0'; //i = c - '0'; if c is the ASCII code for a digit, then i is that digit // blinkled(s[i]-'0'); } return result; } unsigned int stringlength(const char *s) { unsigned int length = 0; for ( ; *s != 0; s++){ // note first part is empty ñ nothing to initialize! length++; // count the characters ñ doesnít include terminating 0 } return length; } void blinkled(int b) { int i; YEL=ON; for (i=0;i<b;i++) { RED=ON; delayms(500); RED=OFF; delayms(500); } delayms(1000); YEL=OFF; } void feeder(const char* s){ unsigned int pwm=0,ms=0; while (*s!=0) { //So long as we dont null terminate. while(*s!=',') { //Convert a number to PWM pwm*=10; pwm+=(*s-'0'); s++; } s++; // "s" will be pointing to a ',', so incriment it once more while(*s!=';') { if (*s==0){ break; } ms*=10; ms+=(*s-'0'); s++; } if(*s!=0x0) s++; // "s" will be pointed to a ';', so incriment if IF you havn't reached the null termination setPulseWidth(pwm); //Set the pwm to the found amount delayms(ms); //Delay by that many miliseconds setPulseWidth(0); //Turn off the motor } } void error(void){ if (err==1){ //printf("\n"); } err=0; } char mygetch(void){ char i; i=getch(); //Get the first char. if(i==0x1B) { //if its an arrow input, return that, otherwise return normaly. getch(); i=(getch() | 0b10000000); //Set MSB } return i; } void blankline(void){ }