8051 Embedded C Traffic Light
Implement a simple traffic-light state machine for an 8051 target.
Project files
Starter — main.c
#include <reg51.h>
sbit RED=P1^0; sbit AMBER=P1^1; sbit GREEN=P1^2;
void main(void){ while(1){ /* TODO states */ }}Completed — main.c
#include <reg51.h>
sbit RED=P1^0; sbit AMBER=P1^1; sbit GREEN=P1^2;
void delay(void){ unsigned int i,j; for(i=0;i<200;i++)for(j=0;j<120;j++); }
void main(void){ while(1){ RED=0;AMBER=1;GREEN=1;delay(); RED=1;AMBER=0;GREEN=1;delay(); RED=1;AMBER=1;GREEN=0;delay(); }}Toolchain and dependencies
IDE requirements
Keil C51, SDCC or compatible 8051 compiler
Compatible targets
AT89S52/8051 trainer
Libraries/dependencies
- Device register header for compiler
Serial output examples
Serial Monitor
No serial output in this starter project.
Compiler-error explanations
reg51.h not found
Likely cause: Compiler/device header is unavailable
Fix: Select an 8051 toolchain and its correct register header
bit-addressable object required
Likely cause: sbit declaration uses an invalid port expression
Fix: Use a bit-addressable SFR pin
Submit your code
Log in to edit the starter files and submit your solution for instructor review.
Log in to submit