CodeCodeCode

ECE 362 LAB08

Nov 8th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.49 KB | None | 0 0
  1. /***********************************************************************
  2. ; ECE 362 - Experiment 8 - Fall 2012                        
  3. ;***********************************************************************
  4. ;***********************************************************************
  5. ;
  6. ; The objective of this experiment is to implement a reaction time assessment
  7. ; tool that measures, with millisecond accuracy, response to a visual
  8. ; stimulus -- here, both a YELLOW LED and the message "Go Team!" displayed on
  9. ; the LCD screen.  The TIM module will be used to generate periodic
  10. ; interrupts every 1.000 ms, to serve as the time base for the reaction measurement.  
  11. ; The RTI module will provide a periodic interrupt at a 2.048 ms rate to serve as
  12. ; a time base for sampling the pushbuttons and incrementing the variable "random"
  13. ; (used to provide a random delay for starting a reaction time test). The SPI
  14. ; will be used to shift out data to an 8-bit SIPO shift register.  The shift
  15. ; register will perform the serial to parallel data conversion for the LCD.
  16. ;
  17. ; The following design kit resources will be used:
  18. ;
  19. ; - left LED (PT1): indicates test stopped (ready to start reaction time test)
  20. ; - right LED (PT0): indicates a reaction time test is in progress
  21. ; - left pushbutton (PAD7): starts reaction time test
  22. ; - right pushbutton (PAD6): stops reaction time test (turns off right LED
  23. ;                    and turns left LED back on, and displays test results)
  24. ; - LCD: displays status and result messages
  25. ; - Shift Register: performs SPI -> parallel conversion for LCD interface
  26. ;
  27. ; When the right pushbutton is pressed, the reaction time is displayed
  28. ; (refreshed in place) on the first line of the LCD as "RT = NNN ms"
  29. ; followed by an appropriate message on the second line
  30. ; e.g., 'Ready to start!' upon reset, 'Way to go HAH!!' if a really
  31. ; fast reaction time is recorded, etc.). The GREEN LED should be turned on
  32. ; for a reaction time less than 250 milliseconds and the RED LED should be
  33. ; turned on for a reaction time greater than 1 second.
  34. ;
  35. ;***********************************************************************/
  36. #include <hidef.h>      /* common defines and macros */
  37. #include "derivative.h"      /* derivative-specific definitions */
  38. #include <mc9s12c32.h>
  39.  
  40. //Additional Defines
  41. #define RIGHT_BTN PTAD_PTAD6 //Right pushbutton
  42. #define LEFT_BTN PTAD_PTAD7 //Left pushbutton
  43. #define RIGHT_LED PTT_PTT0 //Right LED
  44. #define LEFT_LED PTT_PTT1 //Left LED
  45. #define LCD_RS PTT_PTT2 //
  46. #define LCD_RW PTT_PTT3 //
  47. #define LCD_CLK PTT_PTT4 //LCD clock
  48. #define RED_LED PTT_PTT5 //red led
  49. #define YLW_LED PTT_PTT6 //yellow led
  50. #define GRN_LED PTT_PTT7 //green led
  51.  
  52. // All funtions after main should be initialiezed here
  53. char inchar(void);
  54. void outchar(unsigned char);
  55. void tdisp(int, int);
  56. void shiftout(unsigned char);
  57. void lcdwait(void);
  58. void send_byte(unsigned char);
  59. void send_i(unsigned char);
  60. void chgline(int);
  61. void print_c();
  62. void pmsglcd();
  63. void special(void);
  64. unsigned char charlookup(int);
  65.  
  66. //  Variable declarations                                                  
  67. int goteam = 0;   // "go team" flag (used to start reaction timer)
  68. int leftpb  = 0;  // left pushbutton flag
  69. int rightpb = 0;  // right pushbutton flag
  70. int prevrightpb = 0;  // previous right pushbutton state
  71. int prevleftpb = 0;   // previous left pushbutton state
  72. int runstop = 0;  // run/stop flag
  73. int random= 0;  // random variable (2 bytes)
  74. int react   = 0;  // reaction time (3 packed BCD digits)
  75. int tin = 0;    // SCI transmit display buffer IN pointer
  76. int tout    = 0;  // SCI transmit display buffer OUT pointer
  77. int tsize   = 0;  // size of transmit buffer
  78. int tbuf    = 0;  // SCI transmit display buffer
  79. //int cutter = 0;
  80. int cutA = 0;
  81. int cutB = 0;
  82. int cutC = 0;
  83. int test = 0;
  84.  
  85.  
  86. // ASCII character definitions
  87. char CR = 0x0D;//Return  
  88.  
  89. //;LCD COMMUNICATION BIT MASKS
  90. //int RS = 0x04;     //;RS pin mask (PTT[2])
  91. //int RW = 0x08;     //;R/W pin mask (PTT[3])
  92. //int LCDCLK  = 0x10;     //;LCD EN/CLK pin mask (PTT[4])
  93.  
  94. //;LCD INSTRUCTION CHARACTERS
  95. unsigned char LCDON = 0x0F;     //;LCD initialization command
  96. unsigned char LCDCLR = 0x01;     //;LCD clear display command
  97. unsigned char TWOLINE = 0x38;     //;LCD 2-line enable command
  98.  
  99. unsigned char CURMOV = 0xFE;     //;LCD cursor move instruction
  100.  
  101. unsigned char LINE1 = 0x80;     //;LCD line 1 cursor position
  102. unsigned char LINE2 = 0xC0;     //;LCD line 2 cursor position
  103.  
  104. unsigned char line_offset = 0x00;
  105. int cutter = 0;
  106.  
  107. //;LED BIT MASKS
  108. //int GREEN  = 0x20;
  109. //int RED    = 0x40;
  110. //int YELLOW  = 0x80;
  111.                
  112. /***********************************************************************
  113. Initializations
  114. ***********************************************************************/
  115. void  initializations(void) {
  116.  
  117. //; Set the PLL speed (bus clock = 24 MHz)
  118.   CLKSEL = CLKSEL & 0x80; //; disengage PLL from system
  119.   PLLCTL = PLLCTL | 0x40; //; turn on PLL
  120.   SYNR = 0x02;            //; set PLL multiplier
  121.   REFDV = 0;              //; set PLL divider
  122.   while (!(CRGFLG & 0x08)){  }
  123.   CLKSEL = CLKSEL | 0x80; //; engage PLL
  124.  
  125.  
  126. // Disable watchdog timer (COPCTL register)
  127.   COPCTL = 0x40   ; //COP off; RTI and COP stopped in BDM-mode
  128.  
  129. // Initialize asynchronous serial port (SCI) for 9600 baud, no interrupts
  130.   SCIBDH =  0x00; //set baud rate to 9600
  131.   SCIBDL =  0x9C; //24,000,000 / 16 / 156 = 9600 (approx)  
  132.   SCICR1 =  0x00; //$9C = 156
  133.   SCICR2 =  0x0C; //initialize SCI for program-driven operation
  134.   DDRB   =  0x10; //set PB4 for output mode
  135.   PORTB  =  0x10; //assert DTR pin on COM port
  136.          
  137.          
  138. //  Add additional port pin initializations here
  139.   DDRT = 0xFF; //all digital pins as input
  140.   ATDDIEN = 0xC0;
  141.   PTT = 0;
  142.   DDRM = 0xFF;
  143.  
  144. // Initialize the SPI to 6.25 MHz
  145.    //SPICR1_SPE = 1; //SPI enabled
  146.    //SPICR1_SPTIE = 1;
  147.    //SPICR1_LSBFE = 0; //data tranfered most significant bit first
  148.    //SPICR1_MSTR = 1;
  149.    SPICR1 = 0x52;
  150.    //SPIBR_SPR = 0;     //24MHz/4 = 6MHz
  151.    //SPIBR_SPPR = 0x01;
  152.      SPIBR = 0x1;                                                  
  153. // Initialize digital I/O port pins
  154.  
  155.  
  156. /* Initialize the LCD
  157.        ; - pull LCDCLK high (idle)
  158.        ; - pull R/W' low (write state)
  159.        ; - turn on LCD (LCDON instruction)
  160.        ; - enable two-line mode (TWOLINE instruction)
  161.        ; - clear LCD (LCDCLR instruction)
  162.        ; - wait for 2ms so that the LCD can wake up
  163. */
  164.   LCD_CLK = 1; //lcd clk idle
  165.   LCD_RW = 0; //LCD read/write
  166.   special();
  167.   //send_i(LCDON); //turn it on
  168.   //lcdwait();
  169.   //send_i(TWOLINE); //multiline
  170.   //lcdwait();
  171.   //send_i(LCDCLR); //clear it
  172.   //lcdwait();  //wait for LCD to boot
  173.                                                        
  174. // Initialize RTI for 2.048 ms interrupt rate  
  175.   CRGINT = CRGINT | 0x80; //RTI enabled
  176.   RTICTL = 0x1F; //RTI every 2.048ms
  177.  
  178. /*
  179.  Initialize TIM Ch 7 (TC7) for periodic interrupts every 1.000 ms
  180. ;    Enable timer subsystem
  181. ;    Set channel 7 for output compare
  182. ;    Set appropriate pre-scale factor and enable counter reset after OC7
  183. ;    Set up channel 7 to generate 1 ms interrupt rate
  184. ;    Initially disable TIM Ch 7 interrupts
  185. */
  186.   TSCR1_TEN = 1; //TIM enabled
  187.   TIOS_IOS7 = 1; //ch7 set for output compare
  188.   TSCR2_TCRE = 1; //TCNT reset when OC7 occurs
  189.   TSCR2_PR2 = 1; //24MHz/16 = 1.5MHz
  190.   TSCR2_PR1 = 0;
  191.   TSCR2_PR0 = 0;
  192.   TC7 = 1500;   //interrupt every 1ms
  193.   TIE_C7I = 0;  //TIM ch7 interrupt disabled
  194.  
  195.   LEFT_LED = 1; //kk
  196.          
  197. }
  198.                                        
  199. /***********************************************************************
  200. Main
  201. ***********************************************************************/
  202. void main(void) {
  203.   DisableInterrupts;
  204.     initializations();                                 
  205.     EnableInterrupts;
  206.  
  207.   for(;;) {
  208.   //loop
  209.  
  210.   /* write your code here */
  211.  
  212. /*  
  213.   ;  If the left pushbutton ("start reaction test") flag is set, then:
  214. ;    - clear left pushbutton flag
  215. ;    - set the "run/stop" flag
  216. ;    - display message "Ready, Set..." on the first line of the LCD
  217. ;    - turn off the left LED (PT1)
  218. ;    - turn on the right LED (PT0)
  219. ;  Endif
  220. */
  221.    if ((LEFT_BTN == 0) && (leftpb == 1)) {
  222.    //left pb has been pushed
  223.    //Game is ready for input
  224.       leftpb = 0; //toggle left pushbutton flag
  225.       prevleftpb = 0; //toggle previous pushbutton state
  226.      
  227.       if (runstop == 0) {
  228.          runstop = 1;    //start running
  229.          goteam = 0;
  230.          LEFT_LED = 0; //kill left LED
  231.          RIGHT_LED = 1; //on right LED
  232.          tdisp(0,2); //disp for type READYING
  233.       }    
  234.    }
  235.                                                        
  236.  
  237. /*
  238. ;  If the "run/stop" flag is set, then:
  239. ;    - If the "goteam" flag is NOT set, then:
  240. ;       + If "random" = $0000, then:
  241. ;         - set the "goteam" flag
  242. ;         - clear TCNT register (of TIM)
  243. ;         - clear "react" variable (2 bytes)
  244. ;         - enable TIM Ch7 interrupts
  245. ;         - turn on YELLOW LED
  246. ;         - display message "Go Team!" on the second line of the LCD
  247. ;      + Endif
  248. ;    - Endif
  249. ;  Endif
  250. */
  251.    if ((runstop == 1) && (goteam == 0)) {
  252.       if (random == 0) {
  253.         goteam = 1; //react timer start
  254.         TCNT = 0; //clear TCNT
  255.         react = 0;   //reset react timer
  256.         TIE_C7I = 1;  //TIM ch7 interrupt enabled
  257.         YLW_LED = 1;
  258.         RED_LED = 0;
  259.         GRN_LED = 0;
  260.         tdisp(0,1); //disp for type GOING    
  261.       }
  262.    
  263.    }
  264.          
  265. /*
  266. ;  If the right pushbutton ("stop reaction test") flag is set, then:
  267. ;    - clear right pushbutton flag
  268. ;    - clear the "run/stop" flag
  269. ;    - clear the "goteam" flag
  270. ;    - turn off yellow LED
  271. ;    - disable TIM Ch 7 interrupts
  272. ;    - call "tdisp" to display reaction time message
  273. ;    - turn off right LED (PT0)
  274. ;    - turn on left LED (PT1)
  275. ;  Endif
  276. */                                                     
  277.     if ((RIGHT_BTN == 0) && (rightpb == 1) && (goteam == 1)) {
  278.         rightpb = 0;
  279.         prevrightpb = 0;
  280.         runstop = 0;
  281.         goteam = 0;
  282.         //random = 0;
  283.         YLW_LED = 0;
  284.         TIE_C7I = 0;   //TIM ch7 interrupt disabled
  285.         tdisp(react, 0);  //TYPE REACTING
  286.         RIGHT_LED = 0; //right LED on
  287.         LEFT_LED = 1;  //left LED on
  288.         react = 0;
  289.     }
  290.  
  291. /*                                                     
  292. ;  If "react" = 999 (the maximum 3-digit BCD value), then:
  293. ;    - clear the "run/stop" flag
  294. ;    - turn off yellow LED, turn on red LED
  295. ;    - disable TIM Ch 7 interrupts
  296. ;    - display message "Time = 999 ms" on the first line of the LCD
  297. ;    - display message "Too slow!" on the second line of the LCD
  298. ;    - turn off right LED (PT0)
  299. ;    - turn on left LED (PT1)
  300. ;  Endif
  301. */
  302.       if ((react == 999) && (runstop == 1) && (goteam == 1)) {
  303.           runstop = 0;
  304.           YLW_LED = 0;
  305.           RED_LED = 1;
  306.           TIE_C7I = 0;   //TIM ch7 interrupt disabled
  307.           tdisp(react, 0); //TYPE REACTING
  308.           RIGHT_LED = 0;
  309.           LEFT_LED = 1;
  310.           react = 0;
  311.       }
  312.    
  313.     _FEED_COP(); /* feeds the dog */
  314.   } /* loop forever */
  315.   /* please make sure that you never leave main */
  316. }
  317.  
  318.  
  319.  
  320.  
  321. /***********************************************************************                      
  322. ; RTI interrupt service routine: RTI_ISR
  323. ;
  324. ;  Initialized for 2.048 ms interrupt rate
  325. ;
  326. ;  Samples state of pushbuttons (PAD7 = left, PAD6 = right)
  327. ;
  328. ;  If change in state from "high" to "low" detected, set pushbutton flag
  329. ;     leftpb (for PAD7 H -> L), rghtpb (for PAD6 H -> L)
  330. ;     Recall that pushbuttons are momentary contact closures to ground
  331. ;
  332. ;  Also, increments 2-byte variable "random" each time interrupt occurs
  333. ;  NOTE: Will need to truncate "random" to 12-bits to get a reasonable delay                               
  334.  
  335.                
  336. ;***********************************************************************/
  337. interrupt 7 void RTI_ISR(void)
  338. {
  339.     // set CRGFLG bit
  340.     CRGFLG = CRGFLG | 0x80;
  341.    
  342.     if (goteam == 0) {
  343.       random++;
  344.       random = random % 800;
  345.     } else if (goteam == 1) {
  346.       if (random != 0) {
  347.         random--;
  348.       }
  349.     }
  350.    
  351.     if ((RIGHT_BTN == 0) && (prevrightpb == 0)) {//pushed | previously not pushed
  352.            rightpb = 1; //pushed
  353.            
  354.            prevrightpb = 1; //previously pushed
  355.     }
  356.  
  357.     if ((LEFT_BTN == 0) && (prevleftpb == 0)) {//pushed | previously not pushed
  358.            leftpb = 1; //pushed
  359.            prevleftpb = 1; //previously pushed
  360.            
  361.     }
  362.  
  363. }
  364.  
  365. /***********************************************************************                      
  366. ;  TIM interrupt service routine
  367. ;
  368. ;  Initialized for 1.00 ms interrupt rate
  369. ;
  370. ;  Increment (3-digit) variable "react" by one                                             
  371. ;***********************************************************************/
  372. interrupt 15 void TIM_ISR(void)
  373. {
  374.   // set TFLG1 bit
  375.     TFLG1 = TFLG1 | 0x80;
  376.    
  377.     react++;
  378. }
  379.  
  380. void special(){
  381.   LCD_RS = 0;
  382.    
  383.   while (SPISR_SPTEF != 1) {
  384.    
  385.     //aaaaaaaaaaaaa
  386.   }
  387.     if (SPISR_SPTEF == 1) {
  388.        SPIDR = LCDON;
  389.     //SPISR_SPTEF = 0;
  390.        asm { //it's nop time
  391.         nop
  392.         nop
  393.         nop
  394.         nop
  395.         nop
  396.         nop
  397.         nop
  398.         nop
  399.         nop
  400.         nop
  401.         nop
  402.         nop
  403.         nop
  404.         nop
  405.         nop
  406.         nop
  407.         nop
  408.         nop
  409.         nop
  410.         nop
  411.         nop
  412.         nop
  413.         nop
  414.         nop
  415.         nop
  416.         nop
  417.         nop
  418.         nop
  419.         nop
  420.         nop
  421.        }
  422.     }
  423.   LCD_CLK = 1;
  424.   lcdwait();
  425.   LCD_CLK = 0;
  426.   LCD_RS = 1;  
  427.   LCD_RS = 0;
  428.    
  429.   while (SPISR_SPTEF != 1) {
  430.    
  431.     //aaaaaaaaaaaaa
  432.   }
  433.     if (SPISR_SPTEF == 1) {
  434.        SPIDR = TWOLINE;
  435.     //SPISR_SPTEF = 0;
  436.        asm { //it's nop time
  437.         nop
  438.         nop
  439.         nop
  440.         nop
  441.         nop
  442.         nop
  443.         nop
  444.         nop
  445.         nop
  446.         nop
  447.         nop
  448.         nop
  449.         nop
  450.         nop
  451.         nop
  452.         nop
  453.         nop
  454.         nop
  455.         nop
  456.         nop
  457.         nop
  458.         nop
  459.         nop
  460.         nop
  461.         nop
  462.         nop
  463.         nop
  464.         nop
  465.         nop
  466.         nop
  467.        }
  468.     }
  469.   LCD_CLK = 1;
  470.  
  471.   lcdwait();
  472.     LCD_CLK = 0;
  473.   LCD_RS = 1;
  474.   LCD_RS = 0;
  475.    
  476.   while (SPISR_SPTEF != 1) {
  477.    
  478.     //aaaaaaaaaaaaa
  479.   }
  480.     if (SPISR_SPTEF == 1) {
  481.        SPIDR = LCDCLR;
  482.        // SPISR_SPTEF = 0;
  483.        asm { //it's nop time
  484.         nop
  485.         nop
  486.         nop
  487.         nop
  488.         nop
  489.         nop
  490.         nop
  491.         nop
  492.         nop
  493.         nop
  494.         nop
  495.         nop
  496.         nop
  497.         nop
  498.         nop
  499.         nop
  500.         nop
  501.         nop
  502.         nop
  503.         nop
  504.         nop
  505.         nop
  506.         nop
  507.         nop
  508.         nop
  509.         nop
  510.         nop
  511.         nop
  512.         nop
  513.         nop
  514.        }
  515.     }
  516.   LCD_CLK = 1;
  517.  
  518.   lcdwait();
  519.     LCD_CLK = 0;
  520.   LCD_RS = 1;
  521. }
  522. /***********************************************************************                              
  523. ;  tdisp: Display "RT = NNN ms" on the first line of the LCD and display
  524. ;         an appropriate message on the second line depending on the
  525. ;         speed of the reaction.  This routine should use the
  526. ;         "react" variable to determine which number and which message
  527. ;         to display.  pmsglcd will be useful for doing this.
  528. ;     Convert react to ASCII before printing.  
  529. ;        
  530. ;         Also, this routine should set the green LED if the reaction
  531. ;         time was less than 250 ms.
  532. ;
  533. ;         NOTE: The messages should be less than 16 characters since
  534. ;               the LCD is a 2x16 character LCD.
  535. ;***********************************************************************/
  536. void tdisp(int val, int type)
  537. {
  538.     send_i(LCDCLR);
  539.  
  540.     if (type == 0) { //TYPE REACTING
  541.      
  542.       //val is react
  543.       cutC = val % 10;
  544.       val = val / 10;
  545.       cutB = val % 10;
  546.       val = val / 10;
  547.       cutA = val % 10;
  548.    
  549.       //print vals
  550.       chgline(1);
  551.       send_byte(charlookup(cutA));
  552.       chgline(1);
  553.       send_byte(charlookup(cutB));
  554.       chgline(1);
  555.       send_byte(charlookup(cutC));
  556.       chgline(1);
  557.       send_byte(0x6D); //m
  558.       chgline(1);
  559.       send_byte(0x73); //s
  560.       chgline(1);
  561.       send_byte(0x73); //m
  562.       line_offset = 0x00;
  563.    
  564.       if (react <= 250) {
  565.         //print happy things
  566.         chgline(2);
  567.         send_byte(0xCA); //ha
  568.         chgline(2);
  569.         send_byte(0xD4); //ya
  570.         chgline(2);
  571.         send_byte(0xB2); //i
  572.         chgline(2);
  573.         send_byte(0x21); //!
  574.         /*
  575.         send_byte(0x46); //F
  576.         chgline(2);
  577.         send_byte(0x61); //a
  578.         chgline(2);
  579.         send_byte(0x73); //s
  580.         chgline(2);
  581.         send_byte(0x74); //t
  582.         */
  583.         line_offset = 0x00;
  584.        
  585.         RED_LED = 0;
  586.         YLW_LED = 0;
  587.         GRN_LED = 1;
  588.       } else if (react == 999) {
  589.         chgline(2);
  590.         send_byte(0xD2); //me
  591.         chgline(2);
  592.         send_byte(0xDC); //wo
  593.         chgline(2);
  594.         send_byte(0xBB); //sa
  595.         chgline(2);
  596.         send_byte(0xCF); //ma
  597.         chgline(2);
  598.         send_byte(0xBD); //su
  599.         chgline(2);
  600.         send_byte(0x21); //!
  601.         /*
  602.         send_byte(0x57); //W
  603.         chgline(2);
  604.         send_byte(0x61); //a
  605.         chgline(2);
  606.         send_byte(0x6B); //k
  607.         chgline(2);
  608.         send_byte(0x65); //e
  609.         */
  610.         line_offset = 0x00;
  611.        
  612.       } else {
  613.         //print neutral things
  614.         chgline(2);
  615.         send_byte(0x4D); //M
  616.         chgline(2);
  617.         send_byte(0x65); //e
  618.         chgline(2);
  619.         send_byte(0x68); //h
  620.        
  621.         line_offset = 0x00;
  622.       }
  623.     } else if (type == 1) { //TYPE GOING
  624.         chgline(2);
  625.    
  626.         send_byte(0xB2); //i
  627.         chgline(2);
  628.         send_byte(0xB8); //ku
  629.         chgline(2);
  630.         send_byte(0x21); //!
  631.         /*
  632.         send_byte(0x47); //G
  633.         chgline(2);
  634.         send_byte(0x6F); //o
  635.         chgline(2);
  636.         send_byte(0x21); //!
  637.         //chgline(0);
  638.         */
  639.         line_offset = 0x00;
  640.     } else if (type == 2) { //TYPE READYING
  641.         chgline(1);
  642.         send_byte(0x52); //R
  643.         chgline(1);
  644.         send_byte(0x65); //e
  645.         chgline(1);
  646.         send_byte(0x61); //a
  647.         chgline(1);
  648.         send_byte(0x64); //d
  649.         chgline(1);
  650.         send_byte(0x79); //y
  651.         chgline(1);
  652.         send_byte(0x3F); //?
  653.        
  654.         line_offset = 0x00;
  655.     }
  656.       return;
  657. }
  658.  
  659.  
  660. /***********************************************************************                              
  661. ;  look up ascii vals
  662. ;***********************************************************************/
  663. unsigned char charlookup(int anyvar) {
  664.  
  665.   switch(anyvar) {
  666.     case(0):
  667.       return(0x30); //0000 0011
  668.     case(1):
  669.       return(0x31); //0001 0011
  670.     case(2):
  671.       return(0x32); //0010 0011
  672.     case(3):
  673.       return(0x33); //0011 0011
  674.     case(4):
  675.       return(0x34); //0100 0011
  676.     case(5):
  677.       return(0x35); //0101 0011
  678.     case(6):
  679.       return(0x36); //0110 0011
  680.     case(7):
  681.       return(0x37); //0111 0011
  682.     case(8):
  683.       return(0x38); //1000 0011
  684.     case(9):
  685.       return(0x39); //1001 0011
  686.   }
  687. }
  688.  
  689. /***********************************************************************                              
  690. ;  shiftout: Transmits the contents of register A to external shift
  691. ;            register using the SPI.  It should shift MSB first.  
  692. ;            
  693. ;            MISO = PM[4]
  694. ;            SCK  = PM[5]
  695. ;***********************************************************************/
  696. void shiftout(unsigned char output)
  697. {
  698.    while (SPISR_SPTEF != 1) {
  699.    
  700.     //aaaaaaaaaaaaa
  701.    }
  702.     if (SPISR_SPTEF == 1) {
  703.        SPIDR = output;  
  704.        asm { //it's nop time
  705.         nop
  706.         nop
  707.         nop
  708.         nop
  709.         nop
  710.         nop
  711.         nop
  712.         nop
  713.         nop
  714.         nop
  715.         nop
  716.         nop
  717.         nop
  718.         nop
  719.         nop
  720.         nop
  721.         nop
  722.         nop
  723.         nop
  724.         nop
  725.         nop
  726.         nop
  727.         nop
  728.         nop
  729.         nop
  730.         nop
  731.         nop
  732.         nop
  733.         nop
  734.         nop
  735.        }
  736.     }
  737.   //read the SPTEF bit, continue if bit is 1
  738.   //write data to SPI data register
  739.     //wait for 30 cycles for SPI data to shift out
  740. }
  741.  
  742. /***********************************************************************                              
  743. ;  lcdwait: Delay for 2 ms
  744. ;***********************************************************************/
  745. void lcdwait()
  746. {
  747.   int i = 0;
  748.   for (i = 0; i<=2500;i++) {
  749.     asm {//+10cy for 25000cy loop
  750.       nop
  751.       nop
  752.       nop
  753.       nop
  754.       nop
  755.       nop
  756.       nop
  757.       nop
  758.       nop
  759.       nop
  760.     }
  761.   }
  762.  
  763. }
  764.  
  765. /***********************************************************************                              
  766. ;  send_byte: writes contents of register A to the LCD
  767. ;***********************************************************************/
  768. void send_byte(unsigned char character)
  769. {
  770.     //Shift out character
  771.     //Pulse LCD clock line low->high
  772.     //Wait 2 ms for LCD to process data
  773.     shiftout(character);
  774.     LCD_CLK = 0;
  775.     LCD_CLK = 1;
  776.     lcdwait();
  777.    
  778.  
  779. }
  780. /***********************************************************************                              
  781. ;  send_i: Sends instruction passed in register A to LCD  
  782. ;***********************************************************************/
  783. void send_i(unsigned char instruction)
  784. {
  785.     //Set the register select line low (instruction data)
  786.     LCD_RS = 0;
  787.     //Send byte
  788.     send_byte(instruction);
  789.     LCD_RS = 1;
  790. }
  791.  
  792. /***********************************************************************                        
  793. ;  chgline: Move LCD cursor to the cursor position passed in A
  794. ;  NOTE: Cursor positions are encoded in the LINE1/LINE2 variables
  795. ;***********************************************************************/
  796. void chgline(int linetype)
  797. {
  798.     if (linetype == 1) { //first line
  799.         send_i(LINE1 + line_offset);
  800.     } else if (linetype == 2) { // second line
  801.         send_i(LINE2 + line_offset);
  802.     }
  803.    
  804.     line_offset = line_offset + 0x01;
  805. }
  806.  
  807.  
  808. /***********************************************************************                      
  809. ;  print_c: Print character passed in register A on LCD
  810. ;            -> A sub of pmsglcd        
  811. ;***********************************************************************/
  812. void print_c()
  813. {
  814.  
  815. }
  816.  
  817. /***********************************************************************                              
  818. ;  pmsglcd: pmsg, now for the LCD! Expect characters to be passed
  819. ;           by call.  Registers should return unmodified.  Should use
  820. ;           print_c to print characters.  
  821. ;***********************************************************************/
  822. void pmsglcd()
  823. {
  824.  
  825. }
  826.  
  827. /***********************************************************************
  828. ; Character I/O Library Routines for 9S12C32
  829. ;***********************************************************************
  830. ; Name:         inchar
  831. ; Description:  inputs ASCII character from SCI serial port and returns it
  832. ; Example:      char ch1 = inchar();
  833. ;***********************************************************************/
  834. char  inchar(void) {
  835.   /* receives character from the terminal channel */
  836.         while (!(SCISR1 & 0x20)); /* wait for input */
  837.     return SCIDRL;
  838.  
  839. }
  840.  
  841. /***********************************************************************
  842. ; Name:         outchar
  843. ; Description:  outputs ASCII character passed in outchar()
  844. ;                  to the SCI serial port
  845. ; Example:      outchar('x');
  846. ;***********************************************************************/
  847. void outchar(unsigned char ch) {
  848.   /* sends a character to the terminal channel */
  849.     while (!(SCISR1 & 0x80));  /* wait for output buffer empty */
  850.     SCIDRL = ch;
  851. }
Advertisement
Add Comment
Please, Sign In to add comment