简体   繁体   中英

C# Serial Port + CCS C Serial Port + PIC 16F877A

I have a PIC 16F887A connected to the serial port. I want it to lit a green led when it receives 0x01 and lit a red led when it receives 0x00 from pc. I send the characters from a C# windows forms application, the PIC itself is programmed with CCS C. Can you please tell me what i'm doing wrong as the codes below don't work?

Edit: By doesn't work i mean it lits the red led in both cases.

C# Code

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 Code

#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);
   }
}

If it receives 0x00 in both cases, it is likely that you have a baud rate mismatch, even a slight one. After detecting a start bit, the PIC might be seeing the first 7 zeroes and think it saw 8, going you 0x00 in both cases. I would try transmitting from the PIC and PC and watching the lines on a scope to ensure they are running the same speed. You can also try continuously transmitting 0xAA to get an eye pattern (10101010) and comparing the two signals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM