Discussion in "8051 Discussion Forum" started by    paulfjujo    Feb 13, 2018.
Tue Feb 13 2018, 05:58 pm
#1
hello,


i tried this litle code with MikroC for 8051
declare MCU as AT89C2051 FOSC=12MHz
i put R=2,7K as pull up on P2.0 & P3.1 (RX & TX pins)
A led on P3.7 and on P1.7

When i initialise UART_Init (9600); i get error=1 must be 0 if OK ..
Even this status , i pass over it ...then just display the error in hexadecimal after UART_Init
and UART works fine
Displaying correct datas , and able to get data from Keyboard and and display it.
My probleme is on the led P1.7 .. Allways "ON "
.. and all othe P1 pins are "ON"
P3.7 led works fine .

May i forget something ?
Why MikroC tell me Error on UART , even it is working fine ?
Data analyser show UART TX bit width = 106µS instead of 104µS at 9600bds ..so a few baud rate error
(i don't get any answer from MikroC 8051 !)

my cMikroC projetc in zip :







[ Edited Tue Feb 13 2018, 06:00 pm ]
Wed Feb 14 2018, 03:08 am
#2
>When i initialise UART_Init (9600); i get error=1 must be 0 if OK ..

error=1 is baud rate too high.
Software uarts are slower than those in hardware. Try 4800.

"Why MikroC tell me Error on UART , even it is working fine ?"

It may be close enough to work most of the time.

"My problem is on the led P1.7 .. Allways "ON ".. and all othe P1 pins are "ON"

High is the default setting, so you need to set them low.
P1.7 is set high by the error, but not set low again in your code.

8051 pins don't have an "Input" mode like most micros.
They are also only weakly high, they can't source much current.

It is normal to set the pin high and have inputs pull it low.
Because pins are only weakly high, but can sink more current,
it is common to use low as 'on' and arrange the circuit to suit
Wed Feb 14 2018, 07:55 pm
#3
hello,

Thank for your answer,

but today i wrote this short code , to test only P17 and P35 P37
without additional as UART ..
with a led + 390 ohms pull down resistor on this 3 pins.
Led on PORT P1 never react ..
if i initilaise P1=0xFF LedR is allways ON
if in initialise P1=0 LedR is allways OFF
it seems tha Bit command doesnt work .. or where is my mistake ?


#define MCU "AT89C2051"
#define Version "180214"
#define Directory "D:\\_MikroC PRO for 8051\\_MesProjets\\_Test_1er\\"
#define Project "Test_1er_AT89C2051.mcp51"
#define Source "Test3_AT89C2051_3leds.c"


sbit LedV at P3.B7;
sbit LedR at P1.B7;
sbit LedY at P3.B5;

int j;

void main() 
{
//P1=0xFF;   // =>
 LedR allways ON !!
P1=0x00;   // =>
 LedR NEVER  ON !!
P3=0;
do
{

 LedV=0;
 LedR=0;
 LedY=0;
 Delay_ms(1000);
  for (j=0;j<10;j++)
  {
     LedV=1;
     LedR=0;
     LedY=1;
     Delay_ms(1000);
     LedV=0 ;
     LedR=1;
     LedY=0;
     Delay_ms(1000);
     LedY=1 ;
     LedR=0;
     LedV=1;
     Delay_ms(1000);
     LedV=0 ;
     LedY=0;
     LedR=1;
     Delay_ms(1000);
     LedV=1 ;
     LedY=1;
     LedR=1;
      LedV=0 ;
     LedY=0;
     LedR=0;
     Delay_ms(1000);
  }
 }while(1);
}



i am not familiar with this MCU ( i know PIC family)
and find very strange to don't define the way Write or Read..for the MCU Pins
does it means
Write on a pin => pin is an Output
Reading a pin => s pin is an input
maybe too simple to understand .

I just did another test, with a similar aplication, but written is ASM
I used ASM51 to get the *.hex file .. load into AT89C2051
and now , it WORKS FINE .. ( with same hardware ).
asm Program test
;14-02-2018
;Flash_3_LEDS_b.asm
START:  CPL P3.5
		CPL P3.7
		CPL P1.7
ICI:    ACALL WAIT  
        CPL P3.5
		CPL P3.7
		CPL P1.7
        ACALL WAIT
        CPL P1.7
 		ACALL WAIT
 		CPL P3.5
		ACALL WAIT
 		CPL P3.7
        SJMP ICI

WAIT:   MOV R4,#05H
WAIT1:  MOV R3,#00H
WAIT2:  MOV R2,#00H
WAIT3:  DJNZ R2,WAIT3
        DJNZ R3,WAIT2
        DJNZ R4,WAIT1
        RET
        END


so ??? about C version ? or the probleme is ME ?




[ Edited Wed Feb 14 2018, 09:19 pm ]
Thu Feb 15 2018, 02:13 am
#4
>strange to don't define the way Write or Read..for the MCU Pins

Yes it is strange.
The 8051 is the only type without an "Input" mode that I know of.
Probably because the design is so old.

>does it means
>Write on a pin => pin is an Output
>Reading a pin => s pin is an input

The pin can only be in output mode,
but you can read the level on the pin at any time.

So,set the pin high.
Read the pin, you get 1
Force the pin low.. by connecting to ground.
Read the pin, you get 0

8051 pins use internal pull-ups,
so shorting to ground is okay.
Warning .. setting the pin low and forcing high, is not okay.

> just did another test
>...but written is ASM
>...it WORKS FINE
>...so ??? about C version ? or the probleme is ME ?

Really the problem is with the Mikroc compiler.
They decide how to define pins and how to access them.
It may even vary between versions of the same compiler.
In this case the correct way to define a bit is..

sbit LedV at P3_7_bit;



[ Edited Thu Feb 15 2018, 12:16 pm ]
Tue Feb 20 2018, 02:51 pm
#5
Hello

At least, i found myself the response
This following initialisation mode is OK

replace sbit ..
by
#define Switch P3.B4
#define Led P3.B5
and use as output or as input is OK

Then, i successfully build my "Horloge 6 digits" with MikroC for 8051.

Thank's for your answers .

Cordialement
Paul

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

Bobbyerilar
Thu Mar 28 2024, 08:08 am
pb58
Thu Mar 28 2024, 05:54 am
Clarazkafup
Thu Mar 28 2024, 02:24 am
Walterkic
Thu Mar 28 2024, 01:19 am
Davidusawn
Wed Mar 27 2024, 08:30 pm
Richardsop
Tue Mar 26 2024, 10:33 pm
Stevencog
Tue Mar 26 2024, 04:26 pm
Bernardwarge
Tue Mar 26 2024, 11:15 am