|
|
View previous topic :: View next topic |
Author |
Message |
HuynhPhuc
Joined: 08 Dec 2017 Posts: 7
|
Help me! Interfacing PIC 16f877a with esp8266 v1 |
Posted: Fri Dec 08, 2017 9:22 am |
|
|
Hi everybody,
Currently I am doing PIC 16f877a communication with esp8266 v1 (using node-red). I've been transmitting data from PIC to node-red, but when I transmit from node-red to PIC it's not. Although I have used interrupts. Can anyone help me, please? Thanks.
This is my code:
Quote: | #include <16f877a.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232(baud=115200,xmit=pin_C6, rcv=pin_C7)
/********************************/
#define DHT_ER 0
#define DHT_OK 1
#define DHT_ND 0
#define DHT_DA 1
#define DHT PIN_D3
#define RDHT INPUT(PIN_D3)
#define LCD_ENABLE_PIN PIN_D2
#define LCD_RS_PIN PIN_D0
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <lcd.c>
/********************************/
float temp = 0, humid=0;
char const name[]="xxxx", pass[]="xxxxxxxxxxx";
char const server[]="xxx.xxx.xxx.xx", port[]="xxxx";
//char k[30];
int k;
/********************************/
unsigned char DHT_Doc(unsigned char select)
{
unsigned char buffer[5]={0,0,0,0,0};
unsigned char ii,i,checksum;
output_high(DHT);
delay_us(20);
output_low(DHT);
delay_ms(25);
output_high(DHT);
delay_us(40);
if(RDHT==1)return DHT_ER ;
else while(RDHT==0);
delay_us(60);
if(RDHT==0)return DHT_ER;
else while((RDHT==1));
for(i=0;i<5;i++)
{
for(ii=0;ii<8;ii++)
{
while(RDHT==0);
delay_us(50);
if(RDHT==1)
{
buffer[i]|=(1<<(7-ii));
while(RDHT==1);
}
}
}
checksum=buffer[0]+buffer[1]+buffer[2]+buffer[3];
if((checksum)!=buffer[4])return DHT_ER;
if (select==DHT_ND)
{
return(buffer[2]);
}
else if(select==DHT_DA)
{
return(buffer[0]);
}
return DHT_OK;
}
/********************************/
void esp_crlf()
{
printf("\r\n");
delay_ms(1000);
}
/*******************************************************************/
void set_wifi_mode()
{
//printf(lcd_putc,"\f ");
//lcd_gotoxy(1,1);
//printf(lcd_putc,"Setup wifi");
printf("AT+CWMODE_DEF=3");
esp_crlf();
}
/*******************************************************************/
void connect_ap()
{
//printf(lcd_putc,"\f ");
//lcd_gotoxy(1,1);
//printf(lcd_putc,"Wifi: %s",name);
// lcd_gotoxy(1,2);
// printf(lcd_putc,"Connecting...");
printf("AT+CWJAP_DEF=\"%s\",\"%s\"",name,pass);
esp_crlf();
}
/*******************************************************************/
void esp_connect_mode()
{
printf("AT+CIPMUX=0");
esp_crlf();
}
/*******************************************************************/
void connect_to_server()
{
printf("AT+CIPSTART=\"TCP\",\"%s\",%s\r\n",server,port);
delay_ms(1000);
}
/*******************************************************************/
void send_data()
{
connect_to_server();
char str[24]="";
unsigned int8 num;
num=sprintf(str,"t,%2.0f,h,%2.0f,",temp,humid);
printf("AT+CIPSEND=%u\r\n",num);delay_ms(500);
printf("%s\r\n",str);
}
/*******************************************************************/
#INT_RDA
void ngat()
{
k=getch();
}
/*******************************************************************/
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
set_wifi_mode();
esp_connect_mode();
connect_ap();
delay_ms(3000);
while(true)
{
temp= DHT_Doc(DHT_ND);
delay_ms(150);
humid= DHT_Doc(DHT_DA);
delay_ms(150);
lcd_gotoxy(1,1);
printf(lcd_putc,"Humid: %1.0f",humid);
lcd_putC("%");
lcd_gotoxy(1,2);
printf(lcd_putc,"Temp: %1.0f",temp);
lcd_putC(223);
lcd_putC("C");
send_data();
if(k=='1')
{
output_high(pin_c0);
k="";
}
if(k=='0')
{
output_low(pin_c0);
k="";
}
}
} |
This is button on node-red:
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Fri Dec 08, 2017 9:37 am |
|
|
That is a 5 volt PIC, the ESP8266 is a 3 volt device.
Now I have to assume you're actually using an ESP8266 MODULE, not just the 'chip'(there is a HUGE difference) so you need to post a link to the make/model info of what you're using. As a general comment you cannot just 'connect' 3 volt devices to 5 volt PICs.
as for the code... you NEED to add 'ERRORS' to the use RS232(...options...)
Jay |
|
|
HuynhPhuc
Joined: 08 Dec 2017 Posts: 7
|
|
Posted: Fri Dec 08, 2017 9:52 am |
|
|
temtronic wrote: | That is a 5 volt PIC, the ESP8266 is a 3 volt device.
Now I have to assume you're actually using an ESP8266 MODULE, not just the 'chip'(there is a HUGE difference) so you need to post a link to the make/model info of what you're using. As a general comment you cannot just 'connect' 3 volt devices to 5 volt PICs.
as for the code... you NEED to add 'ERRORS' to the use RS232(...options...)
Jay |
I follow this Schematic?
https://circuitdigest.com/fullimage?i=circuitdiagram_mic/PIC16F877A-ESP8266-interfac.png |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Fri Dec 08, 2017 10:24 am |
|
|
Sorry it won't work..
The PIC UART rcv pin is the ST type, which according to the datasheet Electrical charateristic 15.2, parameterD041 requires a MINIMUM of 80% of VDD. Given a VDD of 5 volts, 80% is 4 volts. So it needs at LEAST 4 volts to 'see' a logic '1'. The ESP is a 3 volt device running 3.3 volts in the schematic.
3.3 V is below the 'threshold' or minimum requirement of 4 volts.
Now to make it work ,you have some options.
1) Add logic level conversions. This can be done with discrete FETs, a chip, or a premade 'module'.
2) Use an 'L' version of the PIC. 16LF877 is the 'L'ow voltage version which will run at 3 volts and directly interface to the ESP module.
Others may reply with more options but these are the common ones..
THis is why it is important to read all 300-400-600 oages of a PICs datasheet. I know a LOT of it is boring and technical ,but it is necessary.
Jay |
|
|
HuynhPhuc
Joined: 08 Dec 2017 Posts: 7
|
|
Posted: Fri Dec 08, 2017 11:03 am |
|
|
temtronic wrote: | Sorry it won't work..
The PIC UART rcv pin is the ST type, which according to the datasheet Electrical charateristic 15.2, parameterD041 requires a MINIMUM of 80% of VDD. Given a VDD of 5 volts, 80% is 4 volts. So it needs at LEAST 4 volts to 'see' a logic '1'. The ESP is a 3 volt device running 3.3 volts in the schematic.
3.3 V is below the 'threshold' or minimum requirement of 4 volts.
Now to make it work ,you have some options.
1) Add logic level conversions. This can be done with discrete FETs, a chip, or a premade 'module'.
2) Use an 'L' version of the PIC. 16LF877 is the 'L'ow voltage version which will run at 3 volts and directly interface to the ESP module.
Others may reply with more options but these are the common ones..
THis is why it is important to read all 300-400-600 oages of a PICs datasheet. I know a LOT of it is boring and technical ,but it is necessary.
Jay |
Arduino works at 5v. Why is it working with arduino? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Fri Dec 08, 2017 12:31 pm |
|
|
Please read the Ardunio datasheet as for the electrical specifications for whatever pins are used, compare those to the PIC data I refer to. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19490
|
|
Posted: Fri Dec 08, 2017 1:30 pm |
|
|
The Arduino used TTL input thresholds. The PIC uses Schottky on the SPI. |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Fri Dec 08, 2017 4:01 pm |
|
|
Aside from the electrical mismatch which needs to be corrected, my money is on this:
Code: |
#INT_RDA
void ngat()
{
k=getch();
}
|
No circular buffer to hold the replies..... from my experience with the esp8266, it is quite verbose.
He does say its transmitting so I'm confident the blue smoke is still inside.
His problem in on reception for which he will need a circular buffer. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
HuynhPhuc
Joined: 08 Dec 2017 Posts: 7
|
|
Posted: Fri Dec 08, 2017 11:54 pm |
|
|
temtronic wrote: | Please read the Ardunio datasheet as for the electrical specifications for whatever pins are used, compare those to the PIC data I refer to. |
Thanks very much. I will try again |
|
|
HuynhPhuc
Joined: 08 Dec 2017 Posts: 7
|
|
Posted: Fri Dec 08, 2017 11:54 pm |
|
|
Ttelmah wrote: | The Arduino used TTL input thresholds. The PIC uses Schottky on the SPI. |
Thanks very much |
|
|
HuynhPhuc
Joined: 08 Dec 2017 Posts: 7
|
|
Posted: Sat Dec 09, 2017 12:00 am |
|
|
Gabriel wrote: | Aside from the electrical mismatch which needs to be corrected, my money is on this:
Code: |
#INT_RDA
void ngat()
{
k=getch();
}
|
No circular buffer to hold the replies..... from my experience with the esp8266, it is quite verbose.
He does say its transmitting so I'm confident the blue smoke is still inside.
His problem in on reception for which he will need a circular buffer. |
I am not fluent in english, so I don't understand what you say. Can you send me the RDA interrupt code? Thank you. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19490
|
|
Posted: Sat Dec 09, 2017 1:34 am |
|
|
Look at ex_sisr.c
The problem with what you have, is if a second character arrives before your code has processed the first, it overwrites the first character.... |
|
|
HuynhPhuc
Joined: 08 Dec 2017 Posts: 7
|
|
Posted: Sat Dec 09, 2017 1:45 am |
|
|
Now, I press the "ON" button on the node-red 4-5 times, the C0 pin of the PIC is clicked on the 5v level. The "OFF" button is the same. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9220 Location: Greensville,Ontario
|
|
Posted: Sat Dec 09, 2017 7:09 am |
|
|
this needs to be modified..
#use rs232(baud=115200,xmit=pin_C6, rcv=pin_C7)
you need to add 'ERRORS' to the (...options...)
like this.
#use rs232(baud=115200,xmit=pin_C6, rcv=pin_C7,ERRORS)
The UART can only store 2 incoming characters then it will 'lockup, stall, stop working'. ERRORS adds code to prevent this.
Also you need read how ex_sisr.c works and use it( or a version). |
|
|
|
|
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
|