Código fuente de Stc
El código fuente se transfiere desde: 51 Tutorial de innovación en lenguaje C del microcontrolador.
/*Nombre del experimento: Experimento del semáforo
*Explicación: El experimento del semáforo requiere que la luz roja esté encendida durante 15 segundos, la luz verde durante 10 segundos, y la luz amarilla estará encendida durante 5 segundos.
¿Cuando la luz roja cambia a verde o la luz verde cambia a roja,
? para lograr luces intermitentes. La duración de la iluminación de la luz roja, la luz verde y la luz amarilla se puede modificar a través del puerto serie.
? Y actualice el valor en el siguiente ciclo.
*Autor: Wen Ziqi
*Fecha de revisión: 2010/5/4
*Nota: Para comentarios y explicaciones del código, consulte "51 Microcontrolador "Editado por Wen Ziqi y otros Tutorial de innovación del lenguaje C", Beihang Press.
*/
#¿Contiene? " stc.h "
typedef? ¿No firmado? ¿Ciel? UINT8
typedef? ¿No firmado? int uit 16;
typedef? ¿No firmado? ¿dragón? UINT32
typedef? charINT8
typedef? int? int 16;
typedef? longINT32
#¿Definición? Timer0_Valor inicial? 5000
#Definición? ¿alto? 1
¿#Definición? Bajo 0
¿#Definición? ¿abierto? 1
¿#Definición? OFF0
¿#Definición? ¿SEG_PORT? proteína p0
¿#Definición? ls 164 _ DATOS(x){ si((x))P0 _ 4 = 1 en caso contrario? P0_4=0;}?
#Definición? ¿LS164_CLK(x)? {si((x))P0 _ 5 = 1; en caso contrario? P0_5=0;}?
#Definición? NORTE_R_LIGHT(x)? {si((x))P2 _ 0 = 0; en caso contrario? P2_0=1;}
#Definición? ¿Semáforo Y del Norte (x)? {si((x))P2 _ 1 = 0; en caso contrario? P2_1=1;}
#Definición? ¿Norte_G_Light(x)? {si((x))P2 _ 2 = 0; en caso contrario? P2_2=1;}
#Definición? Luz Nan R (x)? {si((x))P2 _ 3 = 0; en caso contrario? P2_3=1;}
#Definición? Nanguang(x)? {si((x))P2 _ 4 = 0; en caso contrario? P2_4=1;}
#Definición? SUR_G_LIGHT(x)? {si((x))P2 _ 5 = 0; en caso contrario? P2_5=1;}
#Definición? TRAFFIC_STATUS_1?0
¿#Definición? Traffic_status_2?1
¿#Definición? flow_status_3?2
#Definición? UART_MARKER0xEE
uint 8 temporizador 0 evento IRQ = 0;
uint 8 tiempo 1 seg evento = 0
uint 8 tiempo 500 msevent = 0; p>
UART_MARKER0xEE p>
uint 8 cuenta de tiempo = 0;
uint 8 segcurposition = 0
uint 8 lightorgcount[4]= { 15, 5, 15, 5 };
uint 8 lightcurcount[4]= { 15, 5, 15, 5 };
uint 8 Trafficilightstatus = 0; ? ¿UINT8SegCode[10]? ={0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x 90};
UINT8SegBuf[4]? ={0};
¿Código? UINT8SegPosition[4]={0x07, 0x0b, 0x0d, 0x0e};
typedef? estructura? _LIGHT_VAL
{
UINT8? cabeza;?
¿UINT8? valor[4];?
}LIGHT_VAL
typedef? ¿unión? _LIGHT_VAL_EX
{
LIGHT_VAL? LV;
uint 8 p[5];?
}LI
GHT_VAL_EX
¿Nulo? LS164Enviar(UINT8?byte)
{
UINT8? j;
for(j = 0; j & lt=7; j++)
{
if (byte& amp(1 & lt;& lt( 7-j)))
{
LS164_DATA(alto);
? }
? ¿Otros
? {
LS164_DATA(bajo);
? }
LS164_CLK(bajo);?
LS164_CLK(Alto);
}
}
¿No es válido? ¿ActualizarDisplayBuf(UINT8?s1)? //Actualizar la caché de visualización
{
SegBuf[0]? =?s 1% 10;
SegBuf[1]? =?s 1/10;
SegBuf[2]? =?s 1% 10;
SegBuf[3]? =?s 1/10;
}
¿Anulado? Visualización segmentada (vacía)
{
UINT8t
t? =?código seg[seg buf[SegCurPosition]];?
¿SEG_PORT? |=?0x0f
ls 164 enviar(t);?
¿SEG_PORT? =?(SEG_PORT|0x0f)? y amplificador? posición seg[SegCurPosition];
if(++SegCurPosition & gt;=4)?
{
SegCurPosition = 0;
}
}
¿Anulado? TimerInit(no válido)
{
TH1? =?0;
TL1? =?0;?
¿TH0? =?(65536-Temporizador 0_Valor inicial)/256;
TL0? =?(65536-Temporizador 0_Valor inicial)% 256;? //Tiempo 1 milisegundo
TMOD? =?0x 51;/*01010001?Recuento T1, temporización T0*/
}
¿No es válido? Se inicia el temporizador 0 (no válido)
{
¿TR0? =?1;//¿Iniciar el temporizador 1?
? ¿ET0? =?1;
}
¿Anulado? Temporizador 0 detenido (no válido)
{
¿TR0? =?0;//¿Iniciar el temporizador 1?
¿ET0? =?0;
}
¿Anulado? PortInit(nulo)
{
? P0 = p 1 = P2 = P3 = 0x ff;
}
¿No es válido? UartInit(no válido)
{
SCON = 0x 40;
T2CON = 0x34
RCAP2L = 0xD9
RCAP2H = 0xFF
Ren = 1;
ES = 1;
}
¿No es válido? UartSendByte(UINT8?byte)
{
SBUF =byte;
mientras(TI = = 0);
TI = 0 ;
}
¿No es válido? UartPrintfString(INT8?*str)
{
mientras(str?& amp& amp?*str)
{
UartSendB
yte(* str++);
}
}
¿No es válido? Principal(vacío)
{
UINT8? I = 0;
PortInit();
inicio del temporizador();
inicio del temporizador 0(); );
RefreshDisplayBuf(LightCurCount[0]);
EA = 1;
NORTH_R_LIGHT(activado);
SOUTH_G_LIGHT(activada );
mientras(1)
{
si (temporizador 0IRQEvent)
{?
Temporizador 0 evento IRQ = 0;
recuento de tiempo++;
if(recuento de tiempo>=200)
{
recuento de tiempo = 0;
if(LightCurCount[0])
{
traffilightstatus = 0;
}
¿Qué pasa si ? if(LightCurCount[1])
{
traffilightstatus = 1;
}
¿Y si? if(LightCurCount[2])
{?
traffilightstatus = 2;
}
¿Y si? if(LightCurCount[3])
{
traffilightstatus = 3;
}
¿Y si?
{
for(I = 0;i<4;i++)
{
LightCurCount[I]= LightOrgCount[I ];
}
traffilightstatus = 0;
}
Interruptor (estado del semáforo)
{ p>
¿Caso? 0:
{
NORTH_R_LIGHT(encendido);
SOUTH_R_LIGHT(apagado);
NORTH_G_LIGHT(apagado);
SOUTH_G_LIGHT(encendido);
NORTH_Y_LIGHT(apagado);
SOUTH_Y_LIGHT(apagado);
}
Pausa;
¿Caso? 1:
{
if(LightCurCount[1]%2)
{
? NORTH_R_LIGHT(encendido);
SOUTH_G_LIGHT(encendido);
}
¿Otro
{
? NORTH_R_LIGHT(apagado);
SOUTH_G_LIGHT(apagado);
}
NORTH_Y_LIGHT(encendido);
SOUTH_Y_LIGHT(encendido);
}
Rotura;
¿Caso? 2:
{
NORTH_R_LIGHT(apagado);
Sur_R_LIGHT(encendido);
NORTH_G_LIGHT(encendido);
NORTH_G_LIGHT(encendido);
p>
SOUTH_G_LIGHT(apagado);
NORTH_Y_LIGHT(apagado);
SOUTH_Y_LIGHT(apagado);
}
Rotura;
Caso
? 3:
{
if(LightCurCount[3]%2)
{
? NORTH_G_LIGHT(ON);
NORTH_R_LIGHT(ON);
}
Otro
{
? NORTH_G_LIGHT(apagado);
SOUTH_R_LIGHT(apagado);
}
NORTH_Y_LIGHT(encendido);
SOUTH_Y_LIGHT(encendido);
}
Interrupción;
Predeterminado: interrupción
}
RefreshDisplayBuf(LightCurCount[trafficklightstatus]);
LightCurCount[estado del semáforo]-;
}
seg display();
}
}
}
¿Anulado? ¿Interrupción UartIRQ (nula)? 4?
{
¿Electricidad estática? ¿UINT8? CNT = 0;
¿Electricidad estática? ¿LIGHT_VAL_EX? LightValEx
Frecuencia intermedia (RI)
{?
RI = 0;
? luz valex . p[cnt++]= SBUF;
if(LightValEx.lv.Head?==?UART_MARKER)
{
if(CNT & gt ;=5)
{
for(CNT = 1;cnt & lt5;cnt++)
{
LightOrgCount[CNT- 1]= valex ligero .
LightCurCount[CNT-1]= luz LV val[CNT];
}
CNT = 0; UartPrintfString("Establecer semáforo completado\ r \ n ");
}
}
Otros
{
CNT = 0;
}
}
}
¿No es válido? Temporizador 0IRQ(anulado)? ¿interrumpir? 1?
{
ET0 = 0;
TH0? =?(65536-Temporizador 0_Valor inicial)/256;
TL0? =?(65536-Temporizador 0_Valor inicial)% 256;? //Temporizador 1 milisegundo
temporizador 0 IRQ event = 1
ET0 = 1
}
=====; ==================================================== ==============
¡Espera a conseguir puntos!