Cédric SAADAOUI CORREGE
/
mbed_Blinky_PWM_SPI
1st release
main.cpp@0:db5b31dd20a0, 2015-06-17 (annotated)
- Committer:
- CedricSC
- Date:
- Wed Jun 17 13:33:07 2015 +0000
- Revision:
- 0:db5b31dd20a0
1st release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CedricSC | 0:db5b31dd20a0 | 1 | /* |
CedricSC | 0:db5b31dd20a0 | 2 | * INCLUDES |
CedricSC | 0:db5b31dd20a0 | 3 | */ |
CedricSC | 0:db5b31dd20a0 | 4 | #include "mbed.h" |
CedricSC | 0:db5b31dd20a0 | 5 | |
CedricSC | 0:db5b31dd20a0 | 6 | /* |
CedricSC | 0:db5b31dd20a0 | 7 | * CONSTANTES |
CedricSC | 0:db5b31dd20a0 | 8 | */ |
CedricSC | 0:db5b31dd20a0 | 9 | const unsigned char AUDIO_VOLUME[10] = { 0, 28, 56, 84, 112, 140, 168, 197, 226, 255 }; |
CedricSC | 0:db5b31dd20a0 | 10 | |
CedricSC | 0:db5b31dd20a0 | 11 | /* |
CedricSC | 0:db5b31dd20a0 | 12 | * VARIABLES GLOBALES |
CedricSC | 0:db5b31dd20a0 | 13 | */ |
CedricSC | 0:db5b31dd20a0 | 14 | unsigned int global_PWM_Period; |
CedricSC | 0:db5b31dd20a0 | 15 | unsigned int global_PWM_Duty; |
CedricSC | 0:db5b31dd20a0 | 16 | unsigned int global_PWM_PeriodReload; |
CedricSC | 0:db5b31dd20a0 | 17 | unsigned int global_PWM_DutyReload; |
CedricSC | 0:db5b31dd20a0 | 18 | unsigned int global_PWM_Counter; |
CedricSC | 0:db5b31dd20a0 | 19 | unsigned int global_PWM_State; |
CedricSC | 0:db5b31dd20a0 | 20 | |
CedricSC | 0:db5b31dd20a0 | 21 | /* |
CedricSC | 0:db5b31dd20a0 | 22 | * DEFINITION DES MODULES |
CedricSC | 0:db5b31dd20a0 | 23 | */ |
CedricSC | 0:db5b31dd20a0 | 24 | // définitions des LEDs |
CedricSC | 0:db5b31dd20a0 | 25 | //DigitalOut myLed1(LED1); |
CedricSC | 0:db5b31dd20a0 | 26 | DigitalOut myLed2(LED2); |
CedricSC | 0:db5b31dd20a0 | 27 | DigitalOut myLed3(LED3); |
CedricSC | 0:db5b31dd20a0 | 28 | DigitalOut myLed4(LED4); |
CedricSC | 0:db5b31dd20a0 | 29 | // définitions des boutons |
CedricSC | 0:db5b31dd20a0 | 30 | DigitalIn myButton1(p17); |
CedricSC | 0:db5b31dd20a0 | 31 | DigitalIn myButton2(p18); |
CedricSC | 0:db5b31dd20a0 | 32 | DigitalIn myButton3(p19); |
CedricSC | 0:db5b31dd20a0 | 33 | DigitalIn myButton4(p20); |
CedricSC | 0:db5b31dd20a0 | 34 | // définitions du son |
CedricSC | 0:db5b31dd20a0 | 35 | DigitalOut audioCS(p25); |
CedricSC | 0:db5b31dd20a0 | 36 | SPI audioVolume(p5, NC, p7); |
CedricSC | 0:db5b31dd20a0 | 37 | //DigitalOut audioPWM(p26); |
CedricSC | 0:db5b31dd20a0 | 38 | DigitalOut audioPWM(LED1); |
CedricSC | 0:db5b31dd20a0 | 39 | Ticker tickerPWM; |
CedricSC | 0:db5b31dd20a0 | 40 | |
CedricSC | 0:db5b31dd20a0 | 41 | /* |
CedricSC | 0:db5b31dd20a0 | 42 | * INTERRUPTIONS |
CedricSC | 0:db5b31dd20a0 | 43 | */ |
CedricSC | 0:db5b31dd20a0 | 44 | // interruption permettant la génération du PWM |
CedricSC | 0:db5b31dd20a0 | 45 | void IRQ_PWM( void ) |
CedricSC | 0:db5b31dd20a0 | 46 | { |
CedricSC | 0:db5b31dd20a0 | 47 | // si un signal PWM doit etre généré |
CedricSC | 0:db5b31dd20a0 | 48 | if( global_PWM_Period ){ |
CedricSC | 0:db5b31dd20a0 | 49 | // incrémente le compteur |
CedricSC | 0:db5b31dd20a0 | 50 | global_PWM_Counter++; |
CedricSC | 0:db5b31dd20a0 | 51 | // si l'état de la pin de PWM est haute ( duty < counter < period ) |
CedricSC | 0:db5b31dd20a0 | 52 | if( global_PWM_State ){ |
CedricSC | 0:db5b31dd20a0 | 53 | // si le compteur dépasse la période |
CedricSC | 0:db5b31dd20a0 | 54 | if( global_PWM_Counter >= global_PWM_Period ){ |
CedricSC | 0:db5b31dd20a0 | 55 | // mise à 0 du signal |
CedricSC | 0:db5b31dd20a0 | 56 | audioPWM = 0; |
CedricSC | 0:db5b31dd20a0 | 57 | global_PWM_State = 0; |
CedricSC | 0:db5b31dd20a0 | 58 | // reset du counter |
CedricSC | 0:db5b31dd20a0 | 59 | global_PWM_Counter = 0; |
CedricSC | 0:db5b31dd20a0 | 60 | // recharge la période du PWM |
CedricSC | 0:db5b31dd20a0 | 61 | global_PWM_Period = global_PWM_PeriodReload; |
CedricSC | 0:db5b31dd20a0 | 62 | // recharge le rapport cyclique du PWM |
CedricSC | 0:db5b31dd20a0 | 63 | global_PWM_Duty = global_PWM_DutyReload; |
CedricSC | 0:db5b31dd20a0 | 64 | } |
CedricSC | 0:db5b31dd20a0 | 65 | // si l'état de la pîn de PWM est basse ( 0 < counter < duty ) |
CedricSC | 0:db5b31dd20a0 | 66 | }else{ |
CedricSC | 0:db5b31dd20a0 | 67 | // si le compteur dépasse la période |
CedricSC | 0:db5b31dd20a0 | 68 | if( global_PWM_Counter >= global_PWM_Duty ){ |
CedricSC | 0:db5b31dd20a0 | 69 | // mise à 0 du signal |
CedricSC | 0:db5b31dd20a0 | 70 | audioPWM = 1; |
CedricSC | 0:db5b31dd20a0 | 71 | global_PWM_State = 1; |
CedricSC | 0:db5b31dd20a0 | 72 | } |
CedricSC | 0:db5b31dd20a0 | 73 | } |
CedricSC | 0:db5b31dd20a0 | 74 | } |
CedricSC | 0:db5b31dd20a0 | 75 | } |
CedricSC | 0:db5b31dd20a0 | 76 | |
CedricSC | 0:db5b31dd20a0 | 77 | /* |
CedricSC | 0:db5b31dd20a0 | 78 | * SOUS PROGRAMMES |
CedricSC | 0:db5b31dd20a0 | 79 | */ |
CedricSC | 0:db5b31dd20a0 | 80 | void audio_SetVolume( unsigned int volumeIndex ) |
CedricSC | 0:db5b31dd20a0 | 81 | { |
CedricSC | 0:db5b31dd20a0 | 82 | audioCS = 0; |
CedricSC | 0:db5b31dd20a0 | 83 | audioVolume.write( AUDIO_VOLUME[volumeIndex] ); |
CedricSC | 0:db5b31dd20a0 | 84 | audioCS = 1; |
CedricSC | 0:db5b31dd20a0 | 85 | } |
CedricSC | 0:db5b31dd20a0 | 86 | |
CedricSC | 0:db5b31dd20a0 | 87 | void UpdatePWM( unsigned int period, unsigned int duty ) |
CedricSC | 0:db5b31dd20a0 | 88 | { |
CedricSC | 0:db5b31dd20a0 | 89 | global_PWM_PeriodReload = period; |
CedricSC | 0:db5b31dd20a0 | 90 | global_PWM_DutyReload = duty; |
CedricSC | 0:db5b31dd20a0 | 91 | } |
CedricSC | 0:db5b31dd20a0 | 92 | |
CedricSC | 0:db5b31dd20a0 | 93 | void InitPWM( void ) |
CedricSC | 0:db5b31dd20a0 | 94 | { |
CedricSC | 0:db5b31dd20a0 | 95 | // définition de la fonction qui gère le signal PWM (appellée toutes les 10µs) |
CedricSC | 0:db5b31dd20a0 | 96 | tickerPWM.attach_us( &IRQ_PWM, 10 ); |
CedricSC | 0:db5b31dd20a0 | 97 | // reset des variables liées au PWM |
CedricSC | 0:db5b31dd20a0 | 98 | global_PWM_Counter = 0; |
CedricSC | 0:db5b31dd20a0 | 99 | global_PWM_Period = 0; |
CedricSC | 0:db5b31dd20a0 | 100 | global_PWM_PeriodReload = 0; |
CedricSC | 0:db5b31dd20a0 | 101 | global_PWM_Duty = 0; |
CedricSC | 0:db5b31dd20a0 | 102 | global_PWM_DutyReload = 0; |
CedricSC | 0:db5b31dd20a0 | 103 | global_PWM_State = 0; |
CedricSC | 0:db5b31dd20a0 | 104 | audioPWM = 0; |
CedricSC | 0:db5b31dd20a0 | 105 | } |
CedricSC | 0:db5b31dd20a0 | 106 | |
CedricSC | 0:db5b31dd20a0 | 107 | /* |
CedricSC | 0:db5b31dd20a0 | 108 | * FONCTION PRINCIPALE |
CedricSC | 0:db5b31dd20a0 | 109 | */ |
CedricSC | 0:db5b31dd20a0 | 110 | int main() |
CedricSC | 0:db5b31dd20a0 | 111 | { |
CedricSC | 0:db5b31dd20a0 | 112 | // variable locale |
CedricSC | 0:db5b31dd20a0 | 113 | unsigned char i; |
CedricSC | 0:db5b31dd20a0 | 114 | unsigned int delay; |
CedricSC | 0:db5b31dd20a0 | 115 | unsigned char flagStart, flagSens; |
CedricSC | 0:db5b31dd20a0 | 116 | |
CedricSC | 0:db5b31dd20a0 | 117 | // initialise les variables |
CedricSC | 0:db5b31dd20a0 | 118 | i = 0; |
CedricSC | 0:db5b31dd20a0 | 119 | delay = 200; |
CedricSC | 0:db5b31dd20a0 | 120 | flagStart = 1; |
CedricSC | 0:db5b31dd20a0 | 121 | flagSens = 0; |
CedricSC | 0:db5b31dd20a0 | 122 | // boucle de fonctionnement principale |
CedricSC | 0:db5b31dd20a0 | 123 | while(1) { |
CedricSC | 0:db5b31dd20a0 | 124 | /* // si l'application est lancée |
CedricSC | 0:db5b31dd20a0 | 125 | if( flagStart ) |
CedricSC | 0:db5b31dd20a0 | 126 | { |
CedricSC | 0:db5b31dd20a0 | 127 | // éteint toutes les LED |
CedricSC | 0:db5b31dd20a0 | 128 | myLed1 = 1; |
CedricSC | 0:db5b31dd20a0 | 129 | myLed2 = 1; |
CedricSC | 0:db5b31dd20a0 | 130 | myLed3 = 1; |
CedricSC | 0:db5b31dd20a0 | 131 | myLed4 = 1; |
CedricSC | 0:db5b31dd20a0 | 132 | // en fonction de la valeur de i |
CedricSC | 0:db5b31dd20a0 | 133 | switch( i & 0x03 ) |
CedricSC | 0:db5b31dd20a0 | 134 | { |
CedricSC | 0:db5b31dd20a0 | 135 | case 0 : |
CedricSC | 0:db5b31dd20a0 | 136 | myLed1 = 0; |
CedricSC | 0:db5b31dd20a0 | 137 | break; |
CedricSC | 0:db5b31dd20a0 | 138 | case 1 : |
CedricSC | 0:db5b31dd20a0 | 139 | myLed2 = 0; |
CedricSC | 0:db5b31dd20a0 | 140 | break; |
CedricSC | 0:db5b31dd20a0 | 141 | case 2 : |
CedricSC | 0:db5b31dd20a0 | 142 | myLed4 = 0; |
CedricSC | 0:db5b31dd20a0 | 143 | break; |
CedricSC | 0:db5b31dd20a0 | 144 | case 3 : |
CedricSC | 0:db5b31dd20a0 | 145 | myLed3 = 0; |
CedricSC | 0:db5b31dd20a0 | 146 | break; |
CedricSC | 0:db5b31dd20a0 | 147 | } |
CedricSC | 0:db5b31dd20a0 | 148 | // délay d'attente |
CedricSC | 0:db5b31dd20a0 | 149 | wait_ms(delay); |
CedricSC | 0:db5b31dd20a0 | 150 | // si le sens est positif |
CedricSC | 0:db5b31dd20a0 | 151 | if( flagSens ){ |
CedricSC | 0:db5b31dd20a0 | 152 | // incrémente le compteur |
CedricSC | 0:db5b31dd20a0 | 153 | i++; |
CedricSC | 0:db5b31dd20a0 | 154 | // si le sens est négatif |
CedricSC | 0:db5b31dd20a0 | 155 | }else{ |
CedricSC | 0:db5b31dd20a0 | 156 | // décrémente le compteur |
CedricSC | 0:db5b31dd20a0 | 157 | i--; |
CedricSC | 0:db5b31dd20a0 | 158 | } |
CedricSC | 0:db5b31dd20a0 | 159 | } |
CedricSC | 0:db5b31dd20a0 | 160 | */ // si appuie sur bouton 1 |
CedricSC | 0:db5b31dd20a0 | 161 | if(!myButton1){ |
CedricSC | 0:db5b31dd20a0 | 162 | led = led - 0.1; |
CedricSC | 0:db5b31dd20a0 | 163 | // change l'état de marche arret |
CedricSC | 0:db5b31dd20a0 | 164 | flagStart ^= 0x01; |
CedricSC | 0:db5b31dd20a0 | 165 | // attend que le bouton soit relaché |
CedricSC | 0:db5b31dd20a0 | 166 | while( !myButton1 ); |
CedricSC | 0:db5b31dd20a0 | 167 | } |
CedricSC | 0:db5b31dd20a0 | 168 | // si appuie sur bouton 2 |
CedricSC | 0:db5b31dd20a0 | 169 | if(!myButton2){ |
CedricSC | 0:db5b31dd20a0 | 170 | led = led + 0.1; |
CedricSC | 0:db5b31dd20a0 | 171 | // change le sens |
CedricSC | 0:db5b31dd20a0 | 172 | flagSens ^= 0x01; |
CedricSC | 0:db5b31dd20a0 | 173 | // attend que le bouton soit relaché |
CedricSC | 0:db5b31dd20a0 | 174 | while( !myButton2 ); |
CedricSC | 0:db5b31dd20a0 | 175 | } |
CedricSC | 0:db5b31dd20a0 | 176 | // si appuie sur bouton 3 |
CedricSC | 0:db5b31dd20a0 | 177 | if(!myButton3){ |
CedricSC | 0:db5b31dd20a0 | 178 | // réduit le délay de changement |
CedricSC | 0:db5b31dd20a0 | 179 | delay -= 10; |
CedricSC | 0:db5b31dd20a0 | 180 | // attend que le bouton soit relaché |
CedricSC | 0:db5b31dd20a0 | 181 | while( !myButton3 ); |
CedricSC | 0:db5b31dd20a0 | 182 | } |
CedricSC | 0:db5b31dd20a0 | 183 | // si appuie sur bouton 4 |
CedricSC | 0:db5b31dd20a0 | 184 | if(!myButton4){ |
CedricSC | 0:db5b31dd20a0 | 185 | // augmente le délay de changement |
CedricSC | 0:db5b31dd20a0 | 186 | delay += 10; |
CedricSC | 0:db5b31dd20a0 | 187 | // attend que le bouton soit relaché |
CedricSC | 0:db5b31dd20a0 | 188 | while( !myButton4 ); |
CedricSC | 0:db5b31dd20a0 | 189 | } |
CedricSC | 0:db5b31dd20a0 | 190 | } |
CedricSC | 0:db5b31dd20a0 | 191 | } |