12/29/2015

Three phase latching relay programming for PIC Microcontroller 16f873A

During on condition i need to check the continuity of relay contacts. For that i gave +5v supply to the relay contacts output of this contact is given to microcontroller 25th pin. If the relay contact is working, I will increase the relay test count and this count should written into the EEPROM.I f relay contact is not working microcontroller should wait until the contact works.


TEST BSF PORTB,2 ;Set Turn off Relay
CALL DELAY
BCF PORTB,2 ;Clear Turn off Rly
CALL DELAY
CALL DELAY
CALL DELAY
CLRF PORTB
BSF PORTB,1 ;Set Turn on Rly
CALL DELAY
BCF PORTB,1 ;Clear Turn on Rly
CALL DELAY
CALL DELAY
CALL DELAY
MOVF PORTB,W
MOVWF CTAT_STATUS
BTFSS CTAT_STATUS,4
GOTO $-3
MOVLW 01H
MOVWF DATA_EE_ADDR
CALL READ
MOVWF DATA_TCNTL
MOVLW 00H
MOVWF DATA_EE_ADDR
CALL READ
MOVWF DATA_TCNTH
INCFSZ DATA_TCNTL,1
GOTO STORE
INCFSZ DATA_TCNTH,1
GOTO STORE
STORE MOVF DATA_TCNTL,W
MOVWF DATA_EE_DATA
MOVLW 01H
MOVWF DATA_EE_ADDR
CALL WRITE
MOVF DATA_TCNTH,W
MOVWF DATA_EE_DATA
MOVLW 00H
MOVWF DATA_EE_ADDR
CALL WRITE
GOTO TEST


If the code do not work on the application then follow the instructions connect ground to relay contact and check for 0 in program also have a pull up for Microcontroller pin for current program go for pull down resistor that should solve your problem.

Extend PIC Microcontroller‘s RAM

Virtually all PIC microcontrollers have some banking mechanism to extend addressing to additional memory space. But this external data memory is not directly addressable (except in some high versions of PIC18 devices, which include PIC18F8520, PIC18F6620, etc.). In this post we describe easy to implement external memory interface for PIC microcontrollers. Theoretically most of the PIC microcontrollers can use this setup to extend its RAM space. At the prototyping stage we test this system with PIC16F877, PIC16F887, PIC18F4550 and PIC18F4620 microcontrollers.

Schematic

Sample Schematic for Extended RAM system
With this given schematic user may be able to address RAM space up to 192KB. But this can be extended up to 448KB by adding more SRAM modules to the system.
In this given setup we use 24512 – 64K x 8 CMOS SRAMs as a memory modules. 74HC373 latch is used as 8bit to 16bit address extender and 74HC138 demultiplexer is used as memory bank selector.

Function to write data to the external memory bank

//==============================================================================
// Function to write data to the external memory bank
// Input parameters:
//   bank: Memory band number. Valid range is between 1 to 7. 0 for no bank.
//   address: Memory address to write.
//   value: Value to write
// Return:
//   none
//==============================================================================
void WriteExMem(char bank, unsigned int address, char value) {
  asm {
    clrf TRISD
    clrf TRISB
    movlw 0xe0
    andwf TRISC, 1
    movlw 0xc3
    andwf PORTC, 1
    nop
    nop
    movf FARG_WriteExMem_address+1, 0
    movwf PORTD
    movlw 0xfd
    andwf PORTC, 1
    nop
    nop
    bsf PORTC, 1
    movf FARG_WriteExMem_address, 0
    movwf PORTD
    movf FARG_WriteExMem_value, 0
    movwf PORTB
    nop
    nop
    movlw 0xfe
    andwf PORTC, 1
    movf FARG_WriteExMem_bank, 0
    movwf R0
    rlcf R0, 1
    bcf R0, 0
    rlcf R0, 1
    bcf R0, 0
    movf R0, 0
    iorwf PORTC, 1
    nop
    nop
    bsf PORTC, 0
    movlw 0xe3
    andwf PORTC, 1
  }
}

Function to read data from external memory bank


//==============================================================================
// Function to read data from external memory bank
// Input parameters:
//   bank: Memory band number. Valid range is between 1 to 7. 0 for no bank.
//   address: Memory address to read.
// Return:
//   Data value
//==============================================================================
char ReadExMem(char bank, unsigned int address) {
  asm {
    clrf TRISD
    movlw 0xff
    movwf TRISB
    movlw 0xe0
    andwf TRISC, 1
    movlw 0xc3
    andwf PORTC, 1
    nop
    nop
    movf FARG_ReadExMem_address+1, 0
    movwf PORTD
    movlw 0xfd
    andwf PORTC, 1
    nop
    nop
    bsf PORTC, 1
    movf FARG_ReadExMem_address, 0
    movwf PORTD
    movf FARG_ReadExMem_bank, 0
    movwf R0
    rlcf R0, 1
    bcf R0, 0
    rlcf R0, 1
    bcf R0, 0
    movf R0, 0
    iorwf PORTC, 1
    nop
    nop
    movf PORTB, 0
    movwf R1
    movlw 0xe3
    andwf PORTC, 1
    movf R1, 0
    movwf R0
  }
}

Sample Code to test SRAM system


void main() {
  unsigned int address_;
  char bank = 1;
  char txt_buffer[20];
 
  ADCON1 = 15;
  ADCON0 = 0;
  SSPCON1 = 0;
  CCP1CON = 0;
 
  UART1_Init(4800);
  Delay_ms(100);
  UART_Write_Text("Start Testing...\n");
 
  while(bank < 4)
  {
    sprintf(txt_buffer, "Testing bank %c\n", (bank+48));
    UART_Write_Text(txt_buffer);
    for(address_ = 0; address_ < 65535; address_++)
      WriteExMem(bank, address_, 170);           //170 is a magic number to test
    for(address_ = 0; address_ < 65535; address_++)
      if(ReadExMem(bank, address_)!=170)
      {
        UART_Write_Text(" Test FAIL ");
        sprintf(txt_buffer, "address: %u\n", address_);
        UART_Write_Text(txt_buffer);
        bank = 5;
        break;
      }
    if(bank < 5)
      UART_Write_Text(" Test PASS \n");
    bank++;
  }
  UART_Write_Text("SRAM test completed\n");
  while(1);
 
}

The above listed source code is compatible with MikroC Pro for PIC compiler and it can use with other compilers with some slight modifications.

12/26/2015

How to setup OpenCV with Codeblocks on Window XP 32-bit

In this tutorial, i will teach you that how to setup OpenCV with Codeblock on window XP 32-bit.
>> Install the download binary file.
>> After installation the code::block, you will have a working window like this one. 


>> Now, download the openCV library.
>> Install the binary file.
>> Save OpenCV library in your C drive.
>> Double click on it and click on next.
>> After next and agree, chose the following option and select next.



>> Now setup system environment variable. 
>> Open the Start menu. Right click on My Computer and select properties.
>> Click on advance and then click on Environment variable.


>> Create a new variable. Name it OPENCV_DIR and enter variable value C:\OpenCV2.2


>> Find and double click on Path variable.



>> In the variable value add one of the following.

%OPENCV_DIR%\bin

Now, setup the OpenCV with Code::Block.

>>Open Code::Block
>> Goto  setting  >> Compiler and chose Search Directories.
>> Add the following directory 
C:\OpenCV2.2\include\opencv


>> In the link tab add the following.
C:\OpenCV2.2\bin


 Click on the linker setting and add following the lib file.


 That is it. Now you are ready to run OpenCV program. I tested a sample program Contours from C:\OpenCV2.2\samples\c

 

















12/15/2015

Communication Between Microcontroller

The main purpose of communication is to transfer information from source to destination. Communication between Microcontroller have two ways to transmit data on line.

1-Serial Transmission
2-Parallel Transmission
  
Serial Transmission 

When using the Serial Transmission, we transmit only one bit at a time. It simply means there is only one transmission line through which the bit is transmitted.
Serial communication
  
Parallel Transmission 
 
When using the Parallel Transmission, we transmit an entire byte of data at a time. Hence we need 8 lines for transfer 8-bit data.
Parallel communication

In order to understand the Serial communication and the difference between these two. Let's take a look at the following Proteus example.

 >> First Open Proteus ISIS
>> Press on P for choosing component.
>> Search COMPIM
>> In virtual instrument mode select virtual terminal as show below.


>> Make connection as shown below.

 
 >>  Now double click on the COMPIM and set the properties as shown below.

 >> Here , i have selected COM1 and my baud rate is 9600.
>> Similarly open the property of the virtual terminal and set the baud rate is 9600.
>> Now run the simulation.

>> By pressing any key from your keyboard, you will see the TXD pin will be high, which indicate that one bit is transfor as shown below.

In the next tutorial we will see parallel transmission.

  

                             Previous <<             >>Next

12/13/2015

Interfacing LCD with PIC 16F877A Microcontroller

In this tutorial we will see how to interface LCD 16x2 with PIC 16F877A. It is an inevitable part used in all embedded  project. If you will learn it properly then you can easily design embedded project. LCD 16x2 display 2 lines with 16 characters. It can display all the letters, alphabet, mathematical symbols, Greek alphabet etc.  

Related post >> Blinking LED using PIC Microcontroller


Circuit Diagram 

VSS   --> This pin must be connected to the ground.
VDD --> This is positive supply (5v) voltage pin.
VEE --> Contrast adjustment pin
RS   --> Register selection
E     --> Enable
R/W -->  Read/write
D7,D6,D5,D4 --> Data pin




LCD may be connected to the PIC using either 4-bit or 8-bit. Here we are using 4-bit configuration. If an 8-bit bus is used then the remaining pin D1,D2,D3,D4 will be connect with the PIC PORD.D pin

Program:

Device 16F877A
XTAL 20  
ALL_DIGITAL TRUE
Declare LCD_DTPIN PORTD.4
Declare LCD_RSPIN PORTD.2
Declare LCD_ENPIN PORTD.3
Declare LCD_INTERFACE 4
Declare lcd_line 2
Declare LCD_TYPE 0 
Output PORTD
initial:
Cls
Print At 1, 1," Hello World " '
DelayMS 100
GoTo initial
----------------------------------------------------------------------------
 Declare LCD_DTPIN PORTD.4 --> It is uesd for 4 line interface. If we used 8 line interface then we will write 'Declare LCD_DTPIN PORTD.0' 

Declare LCD_RSPIN PORTD.2 --> It tells the compiler that the LCD's RS line will attach to PORTD.2 

Declare LCD_ENPIN PORTD.3--> It tells the compiler that LCD's enable pin will attach to PORTD.3 

Declare LCD_INTERFACE 4  -->Inform the compiler that it is 4 line interface. For 8-line we will write 'Declare LCD_INTERFACE 8. 

Declare lcd_line 2 --> Inform the compiler that the LCD's have 2 line.

Declare LCD_TYPE 0 --> Imform the compiler that it is alpha type LCD.
The remaining command we have discussed in our previous tutorial.

Diagram Of Final Output

                    Previous<<       >>Next

                                                   

12/11/2015

C language

C language is a high level language. It is developed by Dennis M. Ritchie. C language was first implemented on DEC PD-11 computer.  C language has become a professional language.

Why use C Language ?

It is
  • Easy to learn. 
  • Produce efficient programs.
  • Can be compiled on various computer platforms.

C program

Let's look at our first program.


Line 1:
        #include directive tells the C compiler to include stdio.h file.

Line 2:
          main() is the function, where the program execution start.
 
Line 3:
           Curly Braces { tells the compiler, where the main program start.

Line 4:
            Printf() is a function which print the message contain in parenthesis, 

Line 5:
           Curly Braces } tells the compiler that the main program is now end.

C language basically consist of the following main parts.
  1.  Variable
  2.  Function
  3.  Preprocessor command
  4.  Statements
  5. Comments
 Let's understand one by one.

In this example 
Line 1, show preprocessor command.
Line 2, show the main function.
Line 3, show the comment.
Line 4, show the another function.

You can watch this on video





                                           Click here to next >>

12/07/2015

Blinking LED using PIC Microcontroller

In this tutorial, we will learn, how to burn program in PIC Microcontroller. and we will also see how to use Proton IDE. If you still did not install Proton IDE and Proteus then click the following link and install it.

How to install Proton IDE and Proteus.

If anyone having any problem at any point, ask me in comment. I will try my best to resolve it. 
First of all, open the Proteus ISIS and Proton IDE software. It will look the same as below image.


At this point, i supposed you have learnt my previous tutorial, getting stard with Proteus ISIS.

>> Open the Proton IDE.
>> write program for blinking LED.
>> Copy the below code and past it in Proton IDE.



- - - - - - - - - - - - - -  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  - - -

 ' LED BLINKING CODE

 Device 18F452  ' Tell the compiler about device

 XTAL 20            ' Crystal Frequency

 ALL_DIGITAL TRUE   ' For digital input

 Input PORTB.0      ' Initialize portb.0 as input

 Output PORTD.0     ' Initialize portd.0 as output

 PORTB.0=0          ' set portb.0 as low

 PORTD.0=0          ' Set portd.0 As Low

 initial:             ' for looping

 If PORTB.0==1 Then    'condition

 High PORTD.0

 Else

 Low PORTD.0

 EndIf                  ' for ending if

 GoTo initial           ' For continue loop

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - 



After pasting, compile the program by clicking at the compile, showing below.



Now, it will ask the address, where do you want to save the program. After saving, it will automatically create all related files, including HEX file.



 >> Now, put hex file in the Proteus ISIS.
>> Design exactly the same circuit as shown below.



>> Now, double click at the PIC 18F452.
>> Put HEX file as shown below.



 >> After adding the hex file, now click at the play button.




Now, simulation will run. You will get result as shown below.



That is all for today. In the next tutorial, we will learn about different component, which are commenly used like LCD interfarence, serial communication, motor, CCP etc. So stay tuned and subscribe via email and also like us on facebook. If someone having problemn ask me in comment. I will try my best to explain it. 



                            Previous <<             >>Next

 

Interfacing LCD With Arduino Microcontroller

In this tutorial we are going to  interface a  Liquid Crystal Display (LCD)  with Arduino Microcontroller .  Liquid Crystal Display are us...