Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.68 KB | None | 0 0
  1. #include "Libraries/stm32l1xx_nucleo/stm32l1xx_nucleo.h"
  2.  
  3. #define clockmask 1<<5;
  4. #define datamask 1<<7;
  5. #define max7219load 1<<8;
  6. #define Chip_Load   GPIOA->BSRRH |= max7219load;  //%10000000; CS=0
  7. #define Chip_UnLoad GPIOA->BSRRL |= max7219load;  //%01111111; CS=1
  8.  
  9. void configInterruptionPB0(void);
  10. void configInterruptionUserButton(void);
  11. void EXTI0_IRQHandler(void);
  12. void EXTI15_10_IRQHandler(void);
  13. void temporisation(uint16_t t);
  14. void SendData(uint8_t data);
  15. static void SPI_write(uint8_t add, uint8_t data);
  16.  
  17. uint16_t cpt = 0;
  18. uint16_t nombre = 4532;
  19. uint8_t m,c,d,u;
  20.  
  21. int main(){
  22.  
  23.   /* Réglage de l'horloge sur le périphérique GPIOB et GPIOA*/
  24.   RCC->AHBENR |= RCC_AHBPeriph_GPIOA;
  25.   RCC->AHBENR |= RCC_AHBPeriph_GPIOB;
  26.   /* Configuration du bits PB0, en tant qu'entrée et des bits PA0 à PA15 en tant que sortie */
  27.   GPIOA->MODER |= 0x55555555;
  28.   GPIOB->MODER |= 0x00000000;
  29.  /* Configuration de GPIOA et GPIOB en mode Push/Pull */
  30.   GPIOA->OTYPER |= 0x00000000;
  31.   GPIOB->OTYPER |= 0x00000000;
  32.   /* Configuration de GPIOA et GPIOB en haute vitesse */
  33.   GPIOA->OSPEEDR |= 0xFFFFFFFF;
  34.   GPIOB->OSPEEDR |= 0xFFFFFFFF;
  35.  
  36.   /* Utilisation de 4 digits sur les 8 */
  37.   SPI_write(0x0B,0x03); //Scan Limit (0x0B) = 0x03;
  38.   /* Réglage de l'intensité d'affichage */
  39.   SPI_write(0x0A,0x0F); //Intensity (0x0A) = 0x0F;  intensité maximale, voir consommation
  40.   /* Activation du décodage */
  41.   SPI_write(0x09,0xDF); //Decode Mode (0x09) = 0xDF; Décodage pour Digit 0-3  
  42.   /* Écriture des valeurs des digits 0 à 3 */
  43.   //SPI_write(0x01,0x01); //Digit 0 (0x01) = 0x00;
  44.   //SPI_write(0x02,0x01); //Digit 1 (0x02) = 0x01;
  45.   //SPI_write(0x03,0x01); //Digit 2 (0x03) = 0x02;
  46.   //SPI_write(0x04,0x01); //Digit 3 (0x04) = 0x03;
  47.   /* Allumage de l'affichage*/
  48.   //SPI_write(0x0C,0x01); //Shutdown (0x0C)= 0x01;
  49.  
  50.   //configInterruptionPB0();
  51.   configInterruptionUserButton();
  52.  
  53.   while(1){
  54.    
  55.     m = nombre/1000;
  56.     nombre%= 1000;
  57.     c = nombre/100;
  58.     nombre%= 100;
  59.     d = nombre/10;
  60.     nombre%= 10;
  61.     u = nombre;
  62.    
  63.     SPI_write(0x01,m);
  64.     SPI_write(0x02,c);
  65.     SPI_write(0x03,d);
  66.     SPI_write(0x04,u);
  67.     SPI_write(0x0C,0x01);
  68.   }
  69.  
  70.  }
  71.  
  72.  void SendData(uint8_t data)
  73. {
  74. uint8_t LOOP, FLAG;
  75.     GPIOA->BSRRH |= clockmask;//SH_CLK = 0;
  76.  for (LOOP=0; LOOP<8; LOOP++){
  77.     GPIOA->BSRRH |= 1<<5;//SH_CLK = 0;
  78.     FLAG=data&0x80;//because max7219 need MSb first !!
  79.     if(FLAG==0){ GPIOA->BSRRH |= datamask;}// data=0
  80.     else       { GPIOA->BSRRL |= datamask;}// data=1
  81.     data <<= 1;
  82.     GPIOA->BSRRL |= clockmask;//SH_CLK = 1;
  83.     //delay();  
  84.     }
  85.  }
  86.  
  87.  static void SPI_write(uint8_t add, uint8_t data)
  88. {
  89.   Chip_UnLoad;//1
  90.   Chip_Load;//0
  91.   SendData(add);
  92.   SendData(data);
  93.   Chip_UnLoad;//1
  94. }
  95.  
  96.  /* Configuration d'une interruption sur la pin PB0 */
  97.  void configInterruptionPB0(void)
  98. {  
  99.   NVIC->ISER[0] |= (1<<6);
  100.   RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
  101.   SYSCFG->EXTICR[0] |= SYSCFG_EXTICR1_EXTI0_PB;
  102.   EXTI->FTSR |= EXTI_FTSR_TR0;
  103.   EXTI->IMR |= EXTI_IMR_MR0;
  104. }
  105.  
  106. /* Configuration d'une interruption sur le bouton USER PC_13 */
  107.  void configInterruptionUserButton(void)
  108. {  
  109.   NVIC->ISER[1] |= (1<<8);
  110.   RCC -> APB2ENR |= RCC_APB2ENR_SYSCFGEN;
  111.   SYSCFG->EXTICR[3] |=SYSCFG_EXTICR4_EXTI13_PC;
  112.   EXTI->FTSR |= EXTI_FTSR_TR13;
  113.   EXTI->IMR |= EXTI_IMR_MR13;
  114. }
  115.  
  116. /* Routine d'interruption pour le bouton USER PC_13 */
  117. void  EXTI15_10_IRQHandler(void)
  118. {
  119.   EXTI->PR |= EXTI_PR_PR13;
  120.   cpt++;
  121. }
  122.  
  123. /* Routine d'interruption pour PB0 */
  124. void EXTI0_IRQHandler(void)
  125. {
  126.   EXTI->PR |= EXTI_PR_PR0;
  127.   cpt++;
  128. }
  129.  
  130. /* Temporisation de t instructions */
  131. void temporisation(uint16_t t)
  132. {
  133.   uint16_t i;
  134.   for(i=0;i<t;i++)
  135.   {
  136.   }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement