我是嵌入式Linux的新手。我试图在我的芯片上只用UART做一个IR发射器。我设法用单Uart获得38 khz脉冲波。但是为了写出所需的字节来通过红外调整某些东西,我有一些时间问题。例如,我想做1.4 ms逻辑高脉冲和0.5 ms逻辑低(这意味着红外系统的逻辑1帧)所以我的代码就像:
write(fd,buffer,8); //that means 1.4 ms for pulse to be HIGH
usleep(500); // 0.5 ms to wait
然后我想写一个字节,如" 0xF0"所以:
Byte_Write_For_IRDevice(char byte_v)
{
int bitcount;
char t=0;
for(bitcount=0;bitcount<8;bitcount++)
{
int t=byte_v&1;
byte_v>>=1;
if(t==1) //Logic '1' frame
{
write(fd,buffer,8); //that means 1.4 ms for pulse to be HIGH
usleep(500); //0.5 ms for wait
}
else //Logic '0' frame
{
write(fd,buffer,4); //that means 0.7 ms for pulse to be HIGH
usleep(500);
}
}
}
当我调用这个函数时,usleep并不能正常工作。我使用nanosleep,posix线程,但它们不能在这里工作是我的openserialport函数下面。我在哪里犯错误?
int OpenSerialPort()
{
/* Open and configure serial port */
if ((fd = open(SERIALPORT,O_RDWR|O_NOCTTY)) == -1)
{
return -1;
}
struct termios options;
struct serial_struct serinfo;
// generating proper Baudrate to obtain 38 Khz freq. pulse wave on Uart
speed = rate_to_constant(rate);
if (speed == 0) {
/* Custom divisor */
serinfo.reserved_char[0] = 0;
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
return -1;
serinfo.flags &= ~ASYNC_SPD_MASK;
serinfo.flags |= ASYNC_SPD_CUST;
serinfo.custom_divisor = (serinfo.baud_base + (rate / 2)) / rate;
if (serinfo.custom_divisor < 1)
serinfo.custom_divisor = 1;
if (ioctl(fd, TIOCSSERIAL, &serinfo) < 0)
return -1;
if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0)
return -1;
if (serinfo.custom_divisor * rate != serinfo.baud_base) {
warnx("actual baudrate is %d / %d = %f",
serinfo.baud_base, serinfo.custom_divisor,
(float)serinfo.baud_base / serinfo.custom_divisor);
}
}
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, speed ?: B38400);
cfsetospeed(&options, speed ?: B38400);
cfmakeraw(&options);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &options) != 0)
{
//return -1;
}
}