Professional Documents
Culture Documents
Microcontroller
Microcontroller
h"
#include "SYS_INIT.h"
#include "GPIO.h"
#include <stdlib.h>
uint32_t random(){
uint32_t upper = 3, lower = 0;
return (rand() % (upper - lower + 1)) + lower;
cari = cari + 1;
}
cari = 3;
while(cari > 0){
if(loadD>= (cari)) GPIO_WritePin(carD[cari-1], on);
if(loadU>= (cari)) GPIO_WritePin(carU[cari-1], on);
//ms_delay(frame_period/2);
cari = cari - 1;
}
int main(void){
initClock();
sysInit();
/* CLOCK ENABLE */
RCC->AHB1ENR |= 1; /* PORT A */
RCC->AHB1ENR |= 1 << 1; /* PORT B */
RCC->AHB1ENR |= 1 << 2; /* PORT C */
/* CAR RIGHT */
GPIO_InitTypeDef PA5 = {5, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA5);
GPIO_InitTypeDef PA6 = {6, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA6);
GPIO_InitTypeDef PA7 = {7, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA7);
/* CAR RIGHT */
GPIO_InitTypeDef PB13 = {13, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOB}; GPIO_Init(GPIOB, &PB13);
GPIO_InitTypeDef PB14 = {14, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOB}; GPIO_Init(GPIOB, &PB14);
GPIO_InitTypeDef PB15 = {15, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOB}; GPIO_Init(GPIOB, &PB15);
/* CAR DOWN */
GPIO_InitTypeDef PA12 = {12, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA12);
GPIO_InitTypeDef PA11 = {11, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA11);
GPIO_InitTypeDef PA9 = { 9, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA9);
/* CAR UP */
GPIO_InitTypeDef PA10 = {10, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA10);
GPIO_InitTypeDef PB5 = { 5, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOB}; GPIO_Init(GPIOB, &PB5);
GPIO_InitTypeDef PA8 = { 8, GPIO_MODE_OUTPUT, GPIO_NOPULL,
GPIO_SPEED_FREQ_LOW, GPIOA}; GPIO_Init(GPIOA, &PA8);
uint32_t loadL = 1;
uint32_t loadR = 1;
uint32_t loadU = 3;
uint32_t loadD = 3;
// uint32_t loadL = 3;
// uint32_t loadR = 3;
// uint32_t loadU = 2;
// uint32_t loadD = 2;
//
uint32_t loadi, loadLR, loadUD;
uint32_t GreenUDflag = 1;
uint32_t GreenLRflag = 0;
GPIO_WritePin(RedLR, off);
GPIO_WritePin(YellowLR, off);
GPIO_WritePin(GreenLR, off);
GPIO_WritePin(RedUD, off);
GPIO_WritePin(YellowUD, off);
GPIO_WritePin(GreenUD, off);
while(1){
GPIO_WritePin(RedLR, off);
GPIO_WritePin(YellowLR, off);
GPIO_WritePin(GreenLR, off);
GPIO_WritePin(RedUD, off);
GPIO_WritePin(YellowUD, off);
GPIO_WritePin(GreenUD, off);
/* Yellow UD */
GPIO_WritePin(YellowUD, on);
GPIO_WritePin(RedLR, on);
ms_delay(frame_period);
GPIO_WritePin(YellowUD, off);
GPIO_WritePin(RedLR, off);
GPIO_WritePin(GreenLR, on);
GPIO_WritePin(RedUD, on);
ms_delay(frame_period*2);
}
else if(loadUD >= (loadLR + 1)){ /* Load is heavier in up-
down */
GPIO_WritePin(RedLR, off);
GPIO_WritePin(YellowLR, off);
GPIO_WritePin(GreenLR, off);
GPIO_WritePin(RedUD, off);
GPIO_WritePin(YellowUD, off);
GPIO_WritePin(GreenUD, off);
GPIO_WritePin(YellowLR, on );
GPIO_WritePin(RedUD, on );
ms_delay(frame_period);
GPIO_WritePin(YellowLR, off);
GPIO_WritePin(RedUD, off);
GPIO_WritePin(RedLR, on);
GPIO_WritePin(GreenUD, on);
ms_delay(frame_period*2);
}
else{
ms_delay(frame_period*2);
}
//GPIO_WritePin(RedLR, off );
//GPIO_WritePin(RedUD, off );
//GPIO_WritePin(YellowLR, off );
//GPIO_WritePin(YellowUD, off );
//GPIO_WritePin(GreenLR, off );
//GPIO_WritePin(GreenUD, off );
/* LOAD UPDATE */
loadL = loadL - 1;
loadR = loadR - 1;
loadU = loadU - 1;
loadD = loadD - 1;
}
// ms_delay(frame_period*20);
}
}