munther_abdellatif

Untitled

Apr 9th, 2020
172
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. #define CS_Pin  RB2
  3. #define DIN_Pin RB1
  4. #define CLK_Pin RB0
  5.  */
  6. #include <xc.h>
  7. #include "config.h"
  8. #define _XTAL_FREQ 4000000
  9. #include<stdint.h>
  10. #include <stdio.h>
  11. void MAX7219_INIT(void);
  12. void SPI_Write_Byte(unsigned short);
  13. void Read(unsigned short,unsigned short);
  14. void main()
  15. {
  16. TRISB=0x00;
  17. MAX7219_INIT(); // initialize max7219
  18. MAX7219_INIT(); // initialize max7219
  19.    
  20.  while(1){
  21.      Read(1,51);  Read(2,1);  Read(3,51);  Read(4,1);
  22.      Read(5,48);  Read(6,123);Read(7,123); Read(8,91);
  23.     __delay_ms(1000);
  24.      Read(1,123); Read(2,1);  Read(3,51);  Read(4,1);
  25.      Read(5,109); Read(6,126);Read(7,109); Read(8,126);
  26.      __delay_ms(1000);
  27.  }  
  28. }
  29.  
  30. void MAX7219_INIT() {
  31. // Set BCD decode mode
  32. RB2 = 0; // CS pin is pulled LOW
  33. SPI_Write_Byte(0x09); // Select Decode Mode register
  34. SPI_Write_Byte(0x00); // Select BCD mode for digits DIG0-DIG7
  35. RB2 = 1; // CS pin is pulled HIGH
  36.  
  37. // Set display brighness
  38. RB2= 0; // CS pin is pulled LOW
  39. SPI_Write_Byte(0x0A); // Select Intensity register
  40. SPI_Write_Byte(0x05); // Set brightness
  41. RB2 = 1; // CS pin is pulled HIGH
  42.  
  43. // Set display refresh
  44. RB2 = 0; // CS pin is pulled LOW
  45. SPI_Write_Byte(0x0B); // Select Scan-Limit register
  46. SPI_Write_Byte(0x07); // Select digits DIG0-DIG7
  47. RB2 = 1; // CS pin is pulled HIGH
  48.  
  49. // Turn on the display
  50. RB2 = 0; // CS pin is pulled LOW
  51. SPI_Write_Byte(0x0C);
  52. SPI_Write_Byte(0x01);
  53. RB2 = 1; // CS pin is pulled HIGH
  54.  
  55. // Disable Display-Test
  56. RB2= 0; // CS pin is pulled LOW
  57. SPI_Write_Byte(0x0F); // Select Display-Test register
  58. SPI_Write_Byte(0x00); // Disable Display-Test (Normal Operation)
  59. RB2 = 1; // CS pin is pulled HIGH
  60.  
  61. }
  62. void SPI_Write_Byte(unsigned short num) {
  63. unsigned short t, Mask, Flag;
  64. RB0 = 0;
  65. Mask = 128;
  66. for (t=0; t<8; t++) {
  67.     Flag = num & Mask;
  68.     if(Flag == 0) { RB1 = 0; }
  69.     else { RB1 = 1; } RB0 = 1;
  70.     RB0 = 0;
  71.     Mask = Mask >> 1;}
  72. }
  73. void Read(unsigned short digit ,unsigned short data ){
  74. RB2= 0; // CS pin is pulled LOW
  75. SPI_Write_Byte(digit); // Select Display-Test register
  76. SPI_Write_Byte(data); // Disable Display-Test (Normal Operation)
  77. RB2 = 1; // CS pin is pulled HIGH  
  78. }
RAW Paste Data