| 1 | #include <htc.h>
 | 
| 2 | 
 | 
| 3 | __CONFIG(1, FCMDIS & IESODIS & HS);
 | 
| 4 | __CONFIG(2, BORDIS & BORV45 & PWRTEN & WDTDIS & WDTPS1);
 | 
| 5 | __CONFIG(3, CCP2RB3 & LPT1DIS & 0xFDFF);
 | 
| 6 | __CONFIG(4, DEBUGDIS & XINSTDIS & LVPDIS & STVRDIS);
 | 
| 7 | __CONFIG(5, 0xFFFF);
 | 
| 8 | __CONFIG(6, 0xFFFF);
 | 
| 9 | __CONFIG(7, 0xFFFF);
 | 
| 10 | 
 | 
| 11 | #define storage_reg       PORTCbits.RC2
 | 
| 12 | 
 | 
| 13 | 
 | 
| 14 | //prototypes
 | 
| 15 | void wait (unsigned int);
 | 
| 16 | void showInt595(unsigned);
 | 
| 17 | 
 | 
| 18 | 
 | 
| 19 | void main (void)
 | 
| 20 | {
 | 
| 21 | 
 | 
| 22 | unsigned int uValue;
 | 
| 23 |  
 | 
| 24 | /***************************************************************
 | 
| 25 | * 20.07.2011
 | 
| 26 | * spi 74HC595 shift register test
 | 
| 27 | *
 | 
| 28 | * Lines for PIC 18F4550:
 | 
| 29 | * clk: RB1, Pin34
 | 
| 30 | * Ser out, (in for '595): RC7, Pin14
 | 
| 31 | * /G (output enable): RC6 (mußt be low)
 | 
| 32 | * storage register: RC2, Pin12
 | 
| 33 | * RD2: /SCLR clear latch (testing...)
 | 
| 34 | **************************************************************/
 | 
| 35 | // port directions: 1=input, 0=output
 | 
| 36 | 
 | 
| 37 |   PORTB  = 0b00000000;  //clear output data latch
 | 
| 38 |   TRISB  = 0b00000000;  //set to output
 | 
| 39 |  
 | 
| 40 |   PORTC  = 0b00000000;  //clear output data latch
 | 
| 41 |   TRISC  = 0b00000000;  //set to output
 | 
| 42 | 
 | 
| 43 |   PORTD  = 0b00000000;  //clear output data latch
 | 
| 44 |   TRISD  = 0b00000000;  //set to output
 | 
| 45 |   
 | 
| 46 | //while (1) {
 | 
| 47 | PORTCbits.RC0=1; // Einschalt-LED
 | 
| 48 | wait (300);
 | 
| 49 | PORTCbits.RC0=0; // Einschalt-LED
 | 
| 50 | //wait (300);
 | 
| 51 | //}
 | 
| 52 | 
 | 
| 53 |   OpenSPI  (SPI_FOSC_64, MODE_10, SMPMID);
 | 
| 54 | 
 | 
| 55 |   showInt595(0x0001); 
 | 
| 56 |   wait (300);
 | 
| 57 | 
 | 
| 58 |   showInt595(0x0002); 
 | 
| 59 |   wait (300);
 | 
| 60 | 
 | 
| 61 |   showInt595(0x0004); 
 | 
| 62 |   wait (300);
 | 
| 63 | 
 | 
| 64 |   while (1);
 | 
| 65 |  } // Ende main
 | 
| 66 | 
 | 
| 67 | 
 | 
| 68 | //wait
 | 
| 69 | void wait(unsigned int a) {
 | 
| 70 |   unsigned int i,j;
 | 
| 71 |     for (i = 0; i < a ; i++) {
 | 
| 72 |        for (j = 1 ; j < 1000; j++); 
 | 
| 73 |       }
 | 
| 74 | }
 | 
| 75 | 
 | 
| 76 | 
 | 
| 77 | 
 | 
| 78 | /***************************************************************
 | 
| 79 | * 20.07.2011
 | 
| 80 | * 
 | 
| 81 | *
 | 
| 82 | * precondition:
 | 
| 83 | * Lines for PIC 18F4550 mußt be properly initialised
 | 
| 84 | * clk: RB1, Pin34, Pin11 at '595 
 | 
| 85 | * Ser out: RC7, Pin14 at '595
 | 
| 86 | * free selectable: output storage register: RC2 Pin17, Pin12 at '595
 | 
| 87 | *
 | 
| 88 | * /G (output enable): Pin13 on gnd
 | 
| 89 | * /SCLR (clear shift register) Pin10 on Vcc
 | 
| 90 | * /Qh (Pin9) output to the next 595's serial in, input Pin14 
 | 
| 91 | *
 | 
| 92 | * 
 | 
| 93 | **************************************************************/
 | 
| 94 | void showInt595 (unsigned uPattern) 
 | 
| 95 | {
 | 
| 96 | 
 | 
| 97 |   unsigned uDummy1,uDummy2; // = unsigned int
 | 
| 98 |   
 | 
| 99 | 
 | 
| 100 |   unsigned char ucPattern;
 | 
| 101 | 
 | 
| 102 |   //'595 No1
 | 
| 103 |   //die 16bit in 2x 8bit zerlegen, unteres Byte:
 | 
| 104 |   uDummy1 = uPattern;
 | 
| 105 |   uDummy1= uDummy1 & 0x00FF;
 | 
| 106 |   uDummy1 = (unsigned char) uDummy1;  //ist das eigentlich erlaubt bzw. notwendig???
 | 
| 107 |   ucPattern = uDummy1;  
 | 
| 108 |  
 | 
| 109 |   WriteSPI(ucPattern);
 | 
| 110 |   
 | 
| 111 |   //wait (1);           // PIG zu schnell...?
 | 
| 112 |   //storage_reg = 1;    // würde zwar funktionieren, erzeugt aber
 | 
| 113 |   //storage_reg  = 0;   // ein kurzes störendes Blitzen auf den 
 | 
| 114 |                         // korrespondierenden 8 bits
 | 
| 115 |   
 | 
| 116 |   //'595 No2  
 | 
| 117 |   //die 16bit in 2x 8bit zerlegen, oberes Byte:
 | 
| 118 |   uDummy2 = uPattern;
 | 
| 119 |   uDummy2 >>= 8;
 | 
| 120 |   uDummy2 = (unsigned char) uDummy2;
 | 
| 121 |   ucPattern = uDummy2;  
 | 
| 122 | 
 | 
| 123 |   WriteSPI(ucPattern);
 | 
| 124 | 
 | 
| 125 |   wait (1);           // PIG zu schnell...?
 | 
| 126 |    storage_reg = 1;   // alle 16 bits ins Ausgaberegister 
 | 
| 127 |    storage_reg = 0;   // wieder freigeben
 | 
| 128 | }
 |