gpt4 book ai didi

C#串口+CCS C串口+PIC 16F877A

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:16 24 4
gpt4 key购买 nike

我有一个 PIC 16F887A 连接到串行端口。我希望它在接收到 0x01 时点亮绿色 LED,在从电脑接收到 0x00 时点亮红色 LED。我从 C# Windows 窗体应用程序发送字符,PIC 本身是用 CCS C 编程的。你能告诉我我做错了什么吗,因为下面的代码不起作用?

编辑:By 不起作用,我的意思是在这两种情况下它都会点亮红色 LED。

C#代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace deneme
{
public partial class Form1 : Form
{
SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
}

private void openportbtn_Click(object sender, EventArgs e)
{
if (port.IsOpen)
{
port.Close();
}

if (!port.IsOpen)
{
port.Open();
}
}

private void rightbtn_Click(object sender, EventArgs e)
{
byte[] right = new byte[1];
right[0] = 0x01;
port.Write(right, 0, right.Length);
}

private void wrongbtn_Click(object sender, EventArgs e)
{
byte[] wrong = new byte[1];
wrong[0] = 0x00;
port.Write(wrong, 0, wrong.Length);
}
}
}

CCS C代码

#include <16f877A.h>

#fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD

#use delay (clock=4000000)

#use rs232 (baud=9600, xmit=pin_c6, rcv=pin_c7, parity=N, stop=1, bits=8)

char received;
char right = 0x01;

#int_rda
void serial_interrupt()
{
disable_interrupts(int_rda);
received = getc();
if(received == right)
{
output_high(pin_c5); //green led
delay_ms(200);
output_low(pin_c5);
}
else
{
output_high(pin_c4); //red led
delay_ms(200);
output_low(pin_c4);
}
}

void main()
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);

output_low(pin_c4);
output_low(pin_c5);

enable_interrupts(GLOBAL);
while(1)
{
enable_interrupts(int_rda);
}
}

最佳答案

如果在这两种情况下都接收到 0x00,则可能是波特率不匹配,即使是轻微的不匹配。在检测到起始位后,PIC 可能会看到前 7 个零并认为它看到了 8 个,在这两种情况下都为 0x00。我会尝试从 PIC 和 PC 传输并观察示波器上的线路以确保它们运行相同的速度。您也可以尝试连续发送 0xAA 以获得眼图 (10101010) 并比较两个信号。

关于C#串口+CCS C串口+PIC 16F877A,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10338637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com