PIC Program run only during MPLAPX make, but hang otherwise

General discussion on mikroPascal PRO for PIC.
Post Reply
Author
Message
PeetH
Posts: 20
Joined: 25 Mar 2019 19:40

PIC Program run only during MPLAPX make, but hang otherwise

#1 Post by PeetH » 25 Jul 2019 14:21

I could successfully manage to write and compile a small program onto PIC16F887. It runs smoothly without errors directly after I make it with MPLABX via PicKit3. As soon as I disconnect the PicKit3 cable and power up the test unit, the program runs halfway and either stop, or get caught in a loop and I do not understand why. The test program do some ESP8266 setup functions, then establish a TCP connection, send the text "test" to a server that can read it, and runs further in a listening loop. My problem is, that when I want to run the test unit without making it again with MPLABX or just as a standalone without the PicKit3 cable being connected , it runs only up to a line where the LCD screen displays "Set STA+AP mode". See code below - though I do not believe the code could be the problem as it runs through when I make it . I am using MikroPascal Pro 7.2.0 and MPLAB X IDEv5.10 . Any help will be appreciated!


// Get response
function Get_Response() : byte;
begin
if (response_rcvd) then
begin
response_rcvd := 0;
result := responseID;
end
else
result := 0;
end;



begin
{ Main program }
ANSEL := 0; // Configure AN pins as digital I/O
ANSELH := 0;
OSCCON := 0x75;
T1CON := 0x10;

//verstelling van port b volgens die LCD voorbeeld
TRISB := 0; //stel as output om begin waardes toe te ken
PORTB := 0xFF; // ken begin waardes toe
TRISB := 0xFF; // stel nou as input

// serial port setup
TRISC := 0x80; //RC7 = Rx = Input en RC6 = Tx = Output res input
PORTC := 0xFF;
TXSTA := 0xA5; // 0b10100100; set transmitter asyn + enable + 8bit
RCSTA := 0x90; // 0b10010000 set Receiver async + enable + 8 bit
TXREG := 0x00; // inisieer die begin TX waarde

//LED to indicate startup
TRISD := 0x00;
PORTD := 0x00;
SetBit(PortD,0);
Delay_ms(300);
ClearBit(PortD,0);
SetBit(PortD,3);
Delay_ms(300);
ClearBit(PortD,3);
SetBit(PortD,0);
Delay_ms(300);
ClearBit(PortD,0);
SetBit(PortD,3);
Delay_ms(300);
ClearBit(PortD,3);
SetBit(PortD,0);
SetBit(PortD,3);
Delay_ms(1000);
PORTD := 0x00;

txt1 := 'Wifi Example';
txt2 := 'Sending Simple Text';
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
LCD_Out(1,1,txt1); // Write text in first row
LCD_Out(2,1,txt2); // Write text in first row
Delay_ms(1500);

Lcd_Cmd(_LCD_CLEAR); // Clear display
LCD_Out(1,1,'Initialize variables'); // Write text in first row
Delay_ms(1500);
// Initialize variables
state := 0;
response_rcvd := 0;
responseID := 0;
response := 0;
tmp := 0;
i := 0;

SSID := 'Blue Wireless';
password := 'L561929';
port := '5005';


Lcd_Cmd(_LCD_CLEAR); // Clear display
LCD_Out(1,1,'Initialize UART'); // Write text in first row
Delay_ms(1500);
UART1_init(9600); // Initialize UART1 module

RCIE_bit := 1; // Enable UART RX interrupt
PEIE_bit := 1; // Enable Peripheral interrupt
GIE_bit := 1; // Enable Global interrupt

Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,3,'Wait...');
Delay_ms(5000);

// test Configure WiFI module

Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,'Set STA+AP mode');
repeat
begin
UART1_Write_Text('AT+CWMODE=3');
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(500);
end;
until(Get_Response() = OK);
Delay_ms(1000);

Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,'Set connection');
repeat
begin
UART1_Write_Text('AT+CIPMUX=0'); //SINGLE CONNECTION VIR DIE TEST
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(500);
end;
until(Get_Response() = OK);
Delay_ms(1000);


Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,'Connecting to AP');
Lcd_Out(2,1,'Please wait...');
repeat
begin
UART1_Write_Text('AT+CWJAP=');
UART1_Write('"');
UART1_Write_Text(SSID);
UART1_Write('"');
UART1_Write(',');
UART1_Write('"');
UART1_Write_Text(password);
UART1_Write('"');
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(3000);
end;
until(Get_Response() = OK);

Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,'Connected to AP!');

Lcd_Out(2,1,'Getting address');
repeat
begin
UART1_Write_Text('AT+CIFSR');
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(500);
end;
until(Get_Response() = OK);
Delay_ms(1000);


//Start a Single TCP connection
// ip_server := '192.168.1.67';
// StartTCPConnection();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,'Start TCP Connection');
Lcd_Out(2,1,'Please wait...');
repeat
begin
//AT+CIPSTART="TCP","ipadres",port <CR><LF>
UART1_Write_Text('AT+CIPSTART="TCP","192.168.1.67",5005');
{ UART1_Write('"');
UART1_Write_Text('TCP');
UART1_Write('"');
UART1_Write(',');
UART1_Write('"');
UART1_Write_Text(ip_server);
UART1_Write('"');
UART1_Write(',');
UART1_Write_Text(port); }
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(3000);
end;
until(Get_Response() = OK);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,'TCP Started');
Lcd_Out(2,1,'Ready to send and receive...');
Delay_ms(2000);

repeat
begin
UART1_Write_Text('AT+CIPMODE=1');
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(500);
end;
until(Get_Response() = OK);
Lcd_Out(1,1,'Uart Wifi Mode');
Delay_ms(2000);

// Send data
repeat
begin //UART -WIFI PASSTHOUGH
UART1_Write_Text('AT+CIPSEND');
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(500);
UART1_Write_Text('test');
UART1_Write(0x0D);
UART1_Write(0x0A);
Delay_ms(500);
UART1_Write_Text('"+++"'); // OM PASSTHROUGH TE STOP}
Delay_ms(500);
end;
until(Get_Response() = OK);
Lcd_Out(1,1,'SEND Command');
Delay_ms(2000);





state := 100;
i := 0;

// Receive data
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,'Received:');

while TRUE do
begin
if(DataReady = 1) then
begin
DataReady := 0;
Lcd_Out(2, 1, received_data);
end;

Delay_ms(2000);
end;

PeetH
Posts: 20
Joined: 25 Mar 2019 19:40

Re: PIC Program run only during MPLAPX make, but hang otherw

#2 Post by PeetH » 26 Jul 2019 12:43

I find that the program does not hang, but that the ESP does not respond to the AT commands send by the PIC. The rest of the program works - steps that does not involve the ESP-01. Why does it responds and work when I execute the make (program) button of MPLABX, but not when I want to run it without MPLABX.

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: PIC Program run only during MPLAPX make, but hang otherw

#3 Post by filip.grujcic » 26 Jul 2019 15:37

Hello,

I assume there is something wrong with hardware.
What happens when you program the MCU, disconnect the programmer, try the program(unsuccessfully) and then connect the programmer again without programming the MCU. (while having programmer powered on).
Does it work then?

Kind regards,
Filip Grujcic

PeetH
Posts: 20
Joined: 25 Mar 2019 19:40

Re: PIC Program run only during MPLAPX make, but hang otherw

#4 Post by PeetH » 26 Jul 2019 17:27

Thank you Filip. I have tried twice but it still does not work. The first time it just run until it send the first AT command, then I believe it stay in the loop listening for a reply from the ESP. The second time, I put the programmer back quicker, and then freeze, I had to reprogram again for it to work.

I replace the PIC and reprogram, but get exactly the same problem.

Can it be possible, that the UART speed of the PIC is different when programmed and thereafter? Meaning that it have comms at 9600 during the programming process, and then for some reason runs slower or faster thereafter?

Maybe a stupid question, but do I need to add the UART lib in MPLABX?. I just tick off the UART lib/module in the mikroPascal source before I built it.

Regarding the hardware, I made a homemade PCB and solder all components except for the PIC which is in a socket. I use a Logic Level Shifter for the voltage differences, and two separate power supplies capable of providing the currents needed.

I appreciate Filip, hope you can help or guide me in the correct direction.

User avatar
filip.grujcic
Posts: 822
Joined: 14 May 2018 08:34

Re: PIC Program run only during MPLAPX make, but hang otherw

#5 Post by filip.grujcic » 01 Aug 2019 13:49

Hello,

Have you made sure that you set configuration bits correctly in the Edit Project window? (a window that opens when you press Ctrl Shift E).
What is the frequency you specified to the compiler?
It would be helpful if you could zip and attach the project as well.

Kind regards,
Filip Grujcic

PeetH
Posts: 20
Joined: 25 Mar 2019 19:40

Re: PIC Program run only during MPLAPX make, but hang otherw

#6 Post by PeetH » 03 Aug 2019 06:47

filip.grujcic wrote:Hello,

Have you made sure that you set configuration bits correctly in the Edit Project window? (a window that opens when you press Ctrl Shift E).
What is the frequency you specified to the compiler?
It would be helpful if you could zip and attach the project as well.

Kind regards,
Hi Filip, I have checked the config bits, all seems fine.
The frequency specified is 8.00 MHz.
Attached please find the project in zip format.
Your help is much appreciated. I just don't understand while it works when building it, but not when standing alone.
Attachments
Wifi3_Test2.zip
Project files zipped
(131.92 KiB) Downloaded 97 times

PeetH
Posts: 20
Joined: 25 Mar 2019 19:40

Re: PIC Program run only during MPLAPX make, but hang otherw

#7 Post by PeetH » 12 Aug 2019 18:25

filip.grujcic wrote:Hello,

Have you made sure that you set configuration bits correctly in the Edit Project window? (a window that opens when you press Ctrl Shift E).
What is the frequency you specified to the compiler?
It would be helpful if you could zip and attach the project as well.

Kind regards,
Hi Filip I am still puzzled with this project and does not get a way around it. Do not understand why it work when built but not when standing alone.

jpc
Posts: 1986
Joined: 22 Apr 2005 17:40
Location: France 87

Re: PIC Program run only during MPLAPX make, but hang otherw

#8 Post by jpc » 13 Aug 2019 18:35

what is your hardware? Could it be that you have no pull-up on MCLR ?
Au royaume des aveugles, les borgnes sont rois.

PeetH
Posts: 20
Joined: 25 Mar 2019 19:40

Re: PIC Program run only during MPLAPX make, but hang otherw

#9 Post by PeetH » 20 Aug 2019 13:23

jpc wrote:what is your hardware? Could it be that you have no pull-up on MCLR ?
Hi Jpc
I do have a 4k7 resistor on MCLR. Attached please find my hand drawn circuit. The heading is misleading, as I see the program works on its own, but then it stays in the loop when waiting for the ESP-01 to respond. It seems that when programming with MPLAB the ESP responds to the PIC and the steps continue throughout, but when the PIC communicating with the ESP in standalone mode (unit switched off and restarted without the Pickit3 connected or PicKit3 connected but without programming it) it does not work and stays in the loop waiting for the ESP reply OK etc. Hope you can help.
Attachments
WiFi_Circuit.pdf
(95.96 KiB) Downloaded 98 times

PeetH
Posts: 20
Joined: 25 Mar 2019 19:40

Re: PIC Program run only during MPLAPX make, but hang otherwise

#10 Post by PeetH » 15 Apr 2020 15:35

After many months, this problem has been solved. Hardware problem indeed. Replacing the ESP-01 with a new one solved everything.
Thank you to the guys help me looking into the problem.

Post Reply

Return to “mikroPascal PRO for PIC General”