main.c
—
C source code,
2 KB (2625 bytes)
File contents
/* ======================================== * * Michael Smith * Dept. Materials Science and Metallurgy * University of Cambridge * 2016 * * Matrix addressing scheme to control the brightness and flashing rate of 6 LEDs * using 2 PWM signals and 5 pins. * * * ======================================== */ #include <project.h> /* ************** Define variables **************** */ uint16 delay_time = 1; // Delay time in milliseconds, 1 is a reasonable value int P[2][3] = { {250,250,250}, // Enter starting values of brightness from 1 - 254 for each LED {0,100,200} }; int FlashRate[2][3] = { {0,0,0}, // Enter breathing rate. 0 = static {0,0,0} }; /**************************************************************/ int main() { CyGlobalIntEnable; // Enable global interrupts PWM_Start(); PWM_Enable(); for(;;){ // Forever loop int i,j,k; // Local variables to count iterations for(i=0; i<2; i++) { // Nested for loops to cycle through the pattern array, changing the brightness as dictated by the flash rate for(j=0; j<3; j++){ P[i][j] = P[i][j] + FlashRate[i][j]; // Update brightness level of LED if (P[i][j] < 1){ FlashRate[i][j] = - FlashRate[i][j]; // If the brightness is negative, reverse the sign of the flash rate and reset brightness to 1 P[i][j] = 1; } if (P[i][j] > 255){ FlashRate[i][j] = - FlashRate[i][j]; // If the brightness is greater than 255, reverse flash rate and rest brightness value. P[i][j] = 255; } } } for (k=0; k<5; k++){ // Cycles through the LEDs 5 times for each brightness level ColumnsRegister_Write(6); // 110 PWM_WriteCompare1(P[0][0]); PWM_WriteCompare2(P[1][0]); CyDelay(delay_time); // Delay so that the LED has chance to light up ColumnsRegister_Write(5); // 101 PWM_WriteCompare1(P[0][1]); PWM_WriteCompare2(P[1][1]); CyDelay(delay_time); ColumnsRegister_Write(3); // 011 PWM_WriteCompare1(P[0][2]); PWM_WriteCompare2(P[1][2]); CyDelay(delay_time); } } } /* [] END OF FILE */