Advertisement
Guest User

PIC18F27k40 Programming

a guest
Jan 22nd, 2020
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <18F27K40.h>
  2. #fuses NOMCLR RSTOSC_HFINTRC_1MHZ CLKOUT NOWDT NOPROTECT NOCPD
  3. #use delay(internal=64MHZ, clock_out)
  4. #define forever 1       // forever is true
  5.  
  6. struct IOPort_Definition1
  7. {           // 1st structure
  8.    int unused_A;        // Unused pins RA0..RB7
  9.    int ExperimentSelect:4;     // Pins RB0..3 for selection
  10.    int unused_B:4;      // rest of RB
  11.    int1 Single_Bit_LED; // Pin RC0 LED used for single-bit indication
  12.    int unused_C:7;      // Remaining bits of Port C;
  13. };
  14.    
  15. struct IOPort_Definition2
  16. {           // 2nd structure
  17.    int unused_A;        // unused A's for 2nd structure
  18.    int ExpSelect:4;     // Pins RB0..3 for selection
  19.    int unused_B:4;      // rest of RB
  20.    int fourbitOutput:4; // Pin RC0..3 LED
  21.    int unused_C:4;      // Remaining bits of Port C;
  22. };
  23.  
  24. struct IOPort_Definition1 IO_Port;
  25. struct IOPort_Definition1 IO_Direction;
  26. struct IOPort_Definition2 IO_Port_Def2;
  27. struct IOPort_Definition1 IO_Direction_Def2;
  28. //struct IOPort_Definition1 IO_Latch;
  29. #byte IO_Port = 0xF8D       // PORTA register in SFR
  30. #byte IO_Direction = 0xF88  // TRISA register
  31. //#byte IO_Latch = 0xF83
  32.  
  33. void main(void) {  
  34.    IO_Direction.unused_A = 0b00000000;           // all unused outputs
  35.    IO_Direction.ExperimentSelect = 0b1111;              // Pins RA4..7 as output
  36.    IO_Direction.unused_B = 0b0000;               // Pins RB0..7
  37.    IO_Direction.Single_Bit_LED = 0b0;            // Single LED as output
  38.    IO_Direction.unused_C = 0b0000000;            // Unused pins as output
  39.  
  40.    port_a_pullups(0xFF);
  41.    port_b_pullups(0xFF);
  42.    
  43.    setup_oscillator(OSC_HFINTRC_64MHZ);
  44.    
  45.    while(forever)
  46.    {
  47.       switch (IO_Port.ExperimentSelect)
  48.       {
  49.          case 0b0000:
  50.          {
  51.             IO_Port.Single_Bit_LED = 0b0;  // Toggle single LED
  52.             break;
  53.          }
  54.          case 0b1000:
  55.          {
  56.             IO_Port.Single_Bit_LED = ! IO_Port.Single_Bit_LED;  // Toggle single LED
  57.             delay_ms(500); //delays for 0.5 second before restarting loop
  58.             break;
  59.          }              
  60.       }
  61.    }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement