|
|
View previous topic :: View next topic |
Author |
Message |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
Help!!! My PIC does not receive signal from Visual Studio |
Posted: Tue Mar 22, 2011 6:18 am |
|
|
I test the UART with Visual studio 2010 with one button. If the button is press, it will send "A" to PIC. If the PIC receive "A", it will turn on portC0 then the LED will turn ON.
I can see the UART tx pin is lighting when i press the button but then the PIC seem like cant receive it. Can anyone correct the coding for me?
My PIC coding
Quote: | #include <16F877A.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#byte portC=7 // port C map to memory address 7
char rx;
#int_rda
void serial_isr()
{
rx=getch();
if (rx=="A")
{
output_high(pin_C0);
delay_ms(2000);
}
}
void main()
{
set_tris_c(0b10000000);
} |
My Visual Studio using C#
Quote: | 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.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//configuring the serial port
serialPort1.PortName = "COM5";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
//opening the serial port
serialPort1.Open();
//write data to serial port
serialPort1.Write("A");
//close the port
serialPort1.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Tue Mar 22, 2011 6:45 am |
|
|
I'd break it down into two parts...
first, remove the wirng to the PIC andinstall a loopback connector. Now test the Visual Studio program to verify it's workjing the way you want it to.
As for the PIC program..
CCS adds a SLEEP command on the end of 'main' so if you don't use a forever loop ( while(true)).. your program runs through once then the PIC sleeps.....
You have an ISR routine, but never enable interrupts....
Delays within the ISR should NOT be used. Instead, set a flag and get out fast.
Withing Main, test the flag's status..
if set...do the required action,reset the flag ; if not,just keep looping
or do other stuff .
may be other things, but I gotta go fix my forklift... |
|
|
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Tue Mar 22, 2011 6:55 am |
|
|
I dont understand the first part. You mean disconnect the connection of UART in PIC then what is loopback connector?
Where should I add the while(1) ?
I never use ISR before and dont know what is that, I put it in my code cause I get reference online, but I still dont know how to use that. Do you have sample code? Just simple one will do.
Thanks for your reply. |
|
|
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
|
Posted: Tue Mar 22, 2011 12:15 pm |
|
|
#include <16F877A.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#byte portC=7 // port C map to memory address 7
char rx;
#int_rda
void serial_isr()
{
rx=getch();
}
void main()
{
set_tris_c(0b10000000);
enable_interrupts(INT_RDA);
enable_interrupts(INTR_GLOBAL);
while(1) {
if (rx=="A")
{
output_high(pin_C0);
delay_ms(2000);
}
}
Try the above code changes and see if it works. A loop-back connector is a connector you terminate your serial port with that ties the local RXD to TXD and CTS to RTS so it can echo whatever you type on your PC terminal |
|
|
cbarberis
Joined: 01 Oct 2003 Posts: 172 Location: Punta Gorda, Florida USA
|
|
Posted: Tue Mar 22, 2011 12:15 pm |
|
|
Code: |
#include <16F877A.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#byte portC=7 // port C map to memory address 7
char rx;
#int_rda
void serial_isr()
{
rx=getch();
}
void main()
{
set_tris_c(0b10000000);
enable_interrupts(INT_RDA);
enable_interrupts(INTR_GLOBAL);
while(1) {
if (rx=="A")
{
output_high(pin_C0);
delay_ms(2000);
}
} |
Try the above code changes and see if it works. A loop-back connector is a connector you terminate your serial port with that ties the local RXD to TXD and CTS to RTS so it can echo whatever you type on your PC terminal |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Mar 23, 2011 9:55 am |
|
|
Code: | #byte portC=7 // port C map to memory address 7 | This line is not used. My suggestion is to remove the line as it can cause confusion.
For the demonstration it would be nice when the LED would go off again at some condition. For example you could expand the last part of Charberis' program to switch of the LED again when another character is received: Code: | while(1)
{
if (rx=="A")
{
output_high(PIN_C0);
delay_ms(500);
}
else
output_low(PIN_C0);
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|