15,466
社区成员
发帖
与我相关
我的任务
分享
全局变量:
BYTE m_Lamps; // 表示8个灯的高低电平状态
BYTE m_Status[8]; // 表示各个灯的状态
DWORD m_Count; //计数器
主程序:
m_Lamps = 0;
memset(m_Status, 0, sizeof(m_Status));
m_Count = 0;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckFunc, NULL, 0, NULL); //创建子线程
while(1)
{
scanf("%d", &id);
scanf("%d", &newStatus);
m_Status[id] = newStatus;
}
子线程:
DWORD CheckFunc(LPVOID param)
{
while(true)
{
m_Count++;
BYTE b = 0x01;
for (int i=0; i <8; i++)
{
switch (m_Status[i])
{
case 1:
m_Lamps |= b;
break;
case 2:
if ((m_Count / 5) % 2)
m_Lamps |= b;
else
m_Lamps &= ~b;
break;
case 3:
if (m_Count % 2)
m_Lamps |= b;
else
m_Lamps &= ~b;
break;
}
b < <= 1;
}
SetElec(m_Lamps);
Sleep(200); // 睡眠0.2秒
}
return 0;
}
unsigned char LedStatus[10];
void SetLedStatus(unsigned char lIndex,unsigned char status) {
unsigned char ucTmp = 0x01;
unsigned char i;
if(lIndex > 7) lIndex = lIndex % 8;
ucTmp = ucTmp << lIndex;
switch(status) {
case 1:
for(i=0;i<10;i++)
LedStatus[i] = LedStatus[i] | ucTmp;
break;
case 2:
for(i=0;i<5;i++) {
LedStatus[i] = LedStatus[i] | ucTmp;
LedStatus[i+5] = LedStatus[i+5] & (~ucTmp);
}
break;
case 3:
for(i=0;i<5;i++) {
LedStatus[2*i] = LedStatus[2*i] | ucTmp;
LedStatus[2*i+1] = LedStatus[2*i+1] & (~ucTmp);
}
break;
}
}
//一个线程
void DisplayLed() {
unsigned char i;
i=0;
while(1) {
SetElec(LedStatus[i]);
i++;
if(i>9) i=0;
sleep(200);
}
}