Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. #include <p18f4321.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <usart.h>
  5. #pragma config OSC = INTIO2
  6. #pragma config WDT=OFF
  7. #pragma config LVP=OFF
  8. #pragma config BOR =OFF
  9.  
  10. #define SEC_LED PORTEbits.RE0 //sets red LED to RE0
  11. #define NS_RED PORTAbits.RA4 //sets NS RED LED to RA4
  12. #define NS_GREEN PORTAbits.RA5 //sets NS GREEN LED to RA5
  13. #define NS_LT_RED PORTCbits.RC4 //sets NS LT RED LED to RC4
  14. #define NS_LT_GREEN PORTCbits.RC5 //sets NS LT GREEN LED to RC5
  15. #define EW_RED PORTAbits.RA0 //sets EW RED LED to RA0
  16. #define EW_GREEN PORTAbits.RA1 //sets EW GREEN LED to RA1
  17. #define EW_LT_RED PORTAbits.RA2 //sets EW LT RED LED to RA2
  18. #define EW_LT_GREEN PORTAbits.RA3 //sets EW LT GREEN LED to RA3
  19.  
  20. char sevenseg[10] ={0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x18}; //seven segment A-G code
  21.  
  22. void init_UART()
  23. {
  24. OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
  25. USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX &
  26. USART_BRGH_HIGH, 25);
  27. OSCCON = 0x60;
  28. }
  29. void putch(char c) //makes it so the tera tree functions
  30. {
  31. while(!TRMT);
  32. TXREG = c;}
  33.  
  34. void DO_INIT() //step one
  35. {
  36. //ADCON0=0x11; // change to AN4
  37. ADCON1=0x07;//0x19; // select pins AN0 through AN5 as analog signal, VDD-VSS as
  38. //ADCON2=0xA9; // right justify the result. Set the bit conversion time (TAD) and
  39. TRISA = 0xff; //outputs
  40. TRISB = 0x00; //inputs and outputs CHECK!!!!
  41. TRISC = 0x00; //outputs
  42. TRISD = 0x00; //outputs
  43. TRISE = 0x00; //outputs
  44. }
  45.  
  46. void Wait_Half_Second()
  47. {
  48. int I; //define integer i
  49. for(I=0; I<32700; I++); //create delay loop for .5 seconds
  50. }
  51.  
  52. void Wait_One_Second()
  53. {
  54. Wait_Half_Second(); //delay 1/2 a second
  55. SEC_LED = 0; //led off
  56. Wait_Half_Second(); //delay 1/2 a second
  57. SEC_LED = 1; //led on
  58. }
  59.  
  60. void SET_NS(int color)
  61. {int R, G; //integers R and G represent the defined leds
  62. if(color=1){ // case: red= 01
  63. R=1;
  64. G=0;
  65. }
  66. else if(color=2){ // case: yellow= 11
  67. R=1;
  68. G=1;
  69. }
  70. else if(color=3){ // case: green= 10
  71. R=0;
  72. G=1;}
  73. else{ // case: off= 00
  74. R=0;
  75. G=0;
  76. }
  77. NS_RED =R; //set R=RED LED
  78. NS_GREEN =G; //set G=GLED
  79. }
  80. void SET_NS_LT(int color)
  81. {int R, G;
  82. if(color=1){
  83. R=1;
  84. G=0;
  85. }
  86. else if(color=2){
  87. R=1;
  88. G=1;
  89. }
  90. else if(color=3){
  91. R=0;
  92. G=1;}
  93. else{
  94. R=0;
  95. G=0;
  96. }
  97. NS_LT_RED =R;
  98. NS_LT_GREEN =G;
  99. }
  100. void SET_EW(int color)
  101. {int R, G;
  102. if(color=1){
  103. R=1;
  104. G=0;
  105. }
  106. else if(color=2){
  107. R=1;
  108. G=1;
  109. }
  110. else if(color=3){
  111. R=0;
  112. G=1;}
  113. else{
  114. R=0;
  115. G=0;
  116. }
  117. EW_RED =R;
  118. EW_GREEN =G;
  119. }
  120. void SET_EW_LT(int color)
  121. {int R, G;
  122. if(color=1){
  123. R=1;
  124. G=0;
  125. }
  126. else if(color=2){
  127. R=1;
  128. G=1;
  129. }
  130. else if(color=3){
  131. R=0;
  132. G=1;}
  133. else{
  134. R=0;
  135. G=0;
  136. }
  137. EW_LT_RED =R;
  138. EW_LT_GREEN =G;
  139. }
  140.  
  141.  
  142. void Do_Display(void){
  143. int Rint, H, L, abc, R; //define terms for seven segment
  144. if(R<10.0){ //case R is less than 10
  145. H= (int)R; // A point
  146. L= (int)((R-H)*10); // 0."A" point
  147. abc=0;} //DP is off
  148. else{ //R is greater than 9.9999
  149. abc=1; //DP is off
  150. Rint= (int)R; // set A as int
  151. H=Rint/10; // high is Ab
  152. L=Rint%10; //low is aB
  153. }
  154. PORTD = sevenseg[L]; //set portd to l
  155. int I = sevenseg[H]; //insert interger i
  156. PORTC= I&0x0F;
  157. PORTB= ((I&0x70)>>4);
  158. }
  159.  
  160. void main()
  161. {
  162. int i;
  163. DO_INIT();
  164. init_UART();
  165.  
  166. while(1)
  167. {
  168. for (i=0; i<4;i++)
  169. {
  170. SET_NS(i);
  171. SET_NS_LT(i);
  172. SET_EW(i);
  173. SET_EW_LT(i);
  174. Wait_One_Second();
  175. }
  176. //Do_Display();
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement