Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include the library code:
- #include <LiquidCrystal.h>
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- //Mappings of floppy connector
- // <<NAME>> = pin# //<<floppy pin#>> description
- //disabled a few pins to fit it all on an uno..
- //int DSKCHG = 50; //34 Disk Change/Ready
- //int SIDE1 = 31; //32 Head Select
- int RDATA = 6; //30 Read data
- //int WPT = 35; //28 Write protect
- int TRK00 = 7; //26 Track 0
- //int WGATE = 39; //24 Floppy Write Enable
- //int WDATA = 41; //22 Write data
- int STEP = 8; //20 Step
- int DIR = 9; //18 Direction
- //int MOTEB = 47; //16 motor enable a //twisted to 10 by cable twist
- //int DRVSA = 49; //14 select drive a //twisted to 12 by cable twist
- //int DRVSB = 51; //12 select drive b //twisted to 14 by cable twist
- //int MOTEA = 53; //10 motor enable b //twisted to 16 by cable twist
- int INDEX = 10; //08 index
- //plus connect a few of the odd pins to gnd.. (pick some near the center)
- //short MOTEB,MOTEA,DRVSA,DRVSB to gnd, to select 'both' drives..
- //saves a few pins on the arduino...
- int iatoval=0;
- int maxloop=4; //how many times to move the head.
- int maxtrack=80;
- int currenttrack=-1; //set track to -1 to start, will cause us to try to locate track0
- #define IDLE 255
- #define HEADSEEK 0
- #define INITFORREAD 1
- #define TIMEDATA 2
- byte state = HEADSEEK;
- void setup()
- {
- Serial.begin(115200);
- Serial.println("Floppy reader.. test");
- Serial.println();
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- // Print a message to the LCD.
- lcd.print("SETUP.");
- //Outputs
- //pinMode(SIDE1, OUTPUT);
- //pinMode(WGATE, OUTPUT);
- //pinMode(WDATA, OUTPUT);
- pinMode(DIR, OUTPUT);
- pinMode(STEP, OUTPUT);
- //pinMode(MOTEA, OUTPUT);
- //pinMode(MOTEB, OUTPUT);
- //pinMode(DRVSA, OUTPUT);
- //pinMode(DRVSB, OUTPUT);
- //Inputs (all require a pullup)
- pinMode(INDEX, INPUT);
- digitalWrite(INDEX, HIGH);
- pinMode(TRK00, INPUT);
- digitalWrite(TRK00, HIGH);
- // pinMode(WPT, INPUT);
- // digitalWrite(INDEX, HIGH);
- pinMode(RDATA, INPUT);
- digitalWrite(RDATA, HIGH);
- // pinMode(DSKCHG, INPUT);
- // digitalWrite(INDEX, HIGH);
- //Floppy drive uses inverse logic
- // digitalWrite(SIDE1, LOW); //select a head.. don't know which
- // digitalWrite(WGATE, HIGH); //set the write gate high (disables write)
- // //may want to hook this to 5v for safety..
- digitalWrite(DIR, HIGH); //HIGH=toward edge, LOW=toward center
- digitalWrite(STEP, HIGH); // no step at the mo.
- // digitalWrite(MOTEA, HIGH); // stop the drive spinning
- // digitalWrite(MOTEB, LOW); // start the drive spinning
- // digitalWrite(DRVSA, HIGH);
- // digitalWrite(DRVSB, LOW); //select drive b.. pc drives are wired as 'b'
- // //and use cable twist to flip to 'a'
- delay(1000); //wait for spin up.
- lcd.setCursor(0,0);
- lcd.print(" ");
- //jiggle the head to init the trk0 sensor..
- stepForward();
- stepForward();
- stepBackward();
- stepBackward();
- //wait a short while.
- delay(100);
- }
- void stepBackward(){
- digitalWrite(DIR, HIGH);
- digitalWrite(STEP, LOW);
- delayMicroseconds(1);
- digitalWrite(STEP, HIGH);
- delayMicroseconds(3000);
- }
- void stepForward(){
- digitalWrite(DIR, LOW);
- digitalWrite(STEP, LOW);
- delayMicroseconds(1);
- digitalWrite(STEP, HIGH);
- delayMicroseconds(3000);
- }
- boolean idxflip=true;
- long lastidx=-1;
- int indexcount=0;
- void doheadseek(){
- iatoval = !digitalRead(TRK00);
- if(digitalRead(INDEX) != idxflip){
- idxflip = !idxflip;
- long currentmillis = millis();
- if(lastidx==-1)
- lastidx=currentmillis;
- if((currentmillis - lastidx) > 20){ //ignore the idx pulse width, we are tracking the space..
- indexcount++;
- if(indexcount>999)
- indexcount=0;
- lcd.setCursor(7,1);
- lcd.print(indexcount);
- lcd.setCursor(11,1);
- lcd.print( (currentmillis - lastidx) );
- lastidx = currentmillis;
- }
- }
- //debug iato val to lcd
- if(iatoval){
- lcd.setCursor(15,0);
- lcd.print("0");
- }else{
- lcd.setCursor(15,0);
- lcd.print(".");
- }
- if(currenttrack<0 && !iatoval){
- lcd.setCursor(14,0);
- lcd.print("B");
- stepBackward();
- currenttrack=currenttrack-1;
- }
- if(currenttrack<0 && iatoval){
- if(currenttrack<-95){
- lcd.setCursor(14,0);
- lcd.print("f");
- //we have stepped backward enough that our track is now 0.. but TRK0 didnt fire.
- }
- if(iatoval){
- lcd.setCursor(14,0);
- lcd.print("F");
- //we have stepped backward enough that TRK0 has told us where to stop
- }
- currenttrack=0;
- maxloop=maxloop-1;
- if(maxloop==0){
- state=INITFORREAD;
- }
- lcd.setCursor(0,1);
- lcd.print(" 00 ");
- }
- if(maxloop>0)
- {
- if(currenttrack>maxtrack){
- currenttrack=-1; //will flip us to seek backwards..
- lcd.setCursor(14,0);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print("BACK-0");
- }
- if(currenttrack>=0 ){
- //lcd.setCursor(0,0);
- //lcd.print(iatoval);
- lcd.setCursor(0,1);
- lcd.print("TRK ");
- lcd.setCursor(4,1);
- lcd.print(currenttrack);
- stepForward();
- currenttrack=currenttrack+1;
- }
- }
- }
- #define GAPLIST 64
- #define GAPMAX 63
- boolean timerdata=true;
- byte maxcount=32;
- byte rdatacount=0;
- int gap[GAPLIST];
- int *gapptr=&gap[0];
- long newmicros;
- long lastrdata=0;
- boolean rdataflip=true;
- void loop()
- {
- switch(state){
- case HEADSEEK: {
- doheadseek();
- break;
- }
- case INITFORREAD: {
- state = TIMEDATA;
- break;
- }
- case TIMEDATA: {
- //look for state changes on rdata
- //if(digitalRead(RDATA) != rdataflip){
- if( ( PORTD & _BV(RDATA) ) != rdataflip){
- newmicros=micros();
- //record new state
- rdataflip = !rdataflip;
- rdatacount++;
- if(rdatacount>GAPMAX){
- //at some point, we have to dump some data,
- //as the uno doesnt have space to store a track
- //While dumping, we're going to miss data going past
- //so the next gap is going to be a bit on the large side (~16k)
- rdatacount=0; //reset counter for next loop round
- gapptr = &gap[0]; //reset gapptr for data output
- //dump the array to the serial..
- for(int i=0; i<GAPMAX; i++){
- Serial.print(gap[i]);
- Serial.print(" ");
- }
- Serial.print("\n");
- //when maxcount hits zero, we've done enough, for now.
- maxcount--;
- if(maxcount==0){
- state=IDLE;
- }
- }
- *gapptr=newmicros-lastrdata;
- gapptr++;
- lastrdata=newmicros;
- }
- break;
- }
- case IDLE:{
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement