yts‎ > ‎

PIC18F4550

Schematic diagram and Electric circuit


In order to develop the firmware for PIC, download and install MPLAB IDE v8.63 and MPLAB C for PIC18 v3.37.01 in LITE mode (sign in is necessary) to your PC.

Example firmwares for USB are included in Microchip Application Libraries v2010-10-19 (download and install to your PC)


Make a simple circuit board. A schematic diagram and an electric circuit look like


Here, a 20MHz resonator is used for OSC1. After make the circuit, what we should do first is to check if the board works properly.

Let's burn the PIC with a firmware 4550_Wait

1. Place 4550wait folder in d:\ (root directory of d: disk)
2. Double-click 4550wait.mcp or 4550wait.mcw (which is in 4550wait folder) so as to execute MPLAB IDE.
3. Select "Project->Built ALL" from a menu in order to compile and link the firmware.
4. Select "Programmer->Select Programmer" from the menu in order to select your programmer. I have used ICD3 for programmer.
5. Select "Programmer->Program" from the menu in order to burn PIC with the firmware.
6. Select "Programmer->Release From Reset " from the menu in order to run the firmware.

When you run this firmware, the pulses of about 1 sec period ( 5V - 0 V -5V ....) will appears on the #38 pin of PIC. An analog circuit tester can be used to observe these pulses. This appearance means that your PIC circuit works properly. If you look at main.c, you can understand easily what we do. As for a function Delay10KTCYx (), see page 142 of MPLAB C18 Libraries Documentation.

If you dislike the directory structure I use, you can change it (for example, you may change the root directly from D:\ to C:\). In this case, change search-paths properly. Select "Project->Built Options->Project" from the menu so as to get a following dialog window. You can change search-paths (directory structure) to meet your demand. There are 5 Search Paths you should specify.


HID Mouse

Keyword is "PICDEM". As PICDEM(TM) FS-USB is a kit using PIC18F4550, we can utilized the firmwares for it.

1.
We use
C:\Microchip Solutions v2010-10-19\USB Device - HID - Keyboard\Firmware\ USB Device - HID - Mouse - C18 - PICDEM FSUSB.mcw

2.
Double click an icon (USB Device - HID - Mouse - C18 - PICDEM FSUSB.mcw or PICDEM FSUSB.mcp) to execute MPLAB IDE. Select from Menu bar “Project->Save Project As..”. After making a folder named “4550_Mouse” in the root directory of drive D (d:\) and another folder named “Mouse” in 4550_Mouse folder, save the project as “Mouse” . Finish MPLAB IDE.

Then, you can find 2 folders on d:\;



3.
Double click an icon of D:\4550_Mouse\Mouse\ Mouse.mcw (or Mouse.mcp). Build (compile and link) firmwares (select Project->Build All from Menu bar). If there are no errors, you find the words “BUILD SUCCEEDED”

4.
Modify main.c a little bit (minimum modification). This modification is needed, because firmwares are for PICDEM(TM) FS-USB and they include unnecessary codes for our simple PIC board.

If you look at the firmware codes in main.c and find the codes indicated by //YTS, you will understand what I do. As the unnecessary codes still remain, you can remove them if you want to.

5.
Build the obtained firmware and write it in PIC. After connecting PIC to PC by a USB cable, execute the firmware. You will find that a mouse cursor draws a circle on your display of PC.


Place : 4550_Mouse.zip  (modified firmware) and  Microchip.zip in the root directory of drive D (d:\).

HID Keyboard

1.
We use
C:\Microchip Solutions v2010-10-19\USB Device - HID - Keyboard\Firmware\ USB Device - HID - Keyboard- C18 - PICDEM FSUSB.mcw.

2.
Double click an icon (USB Device - HID - Keyboard- C18 - PICDEM FSUSB.mcw) to execute MPLAB IDE. Select from Menu bar “Project->Save Project As..”. After making a folder named “4550_keyboard” in the root directory of drive D (d:\) and another folder named “keyboard” in 4550_keyboard folder, save the project as “keyboard” . Finish MPLAB IDE. Then, you can find 2 folders on d:\; 4550_keyboard and Microchip

3. Double click an icon of D:\4550_keyboard\keyboard\ keyboard.mcw (or keyboard.mcp). Build (compile and link) firmwares (select Project->Build All from Menu bar). If there are no errors, you find the words “BUILD SUCCEEDED”

4.
Modify main.c a little bit (minimum modification). This modification is needed*, because firmwares are for PICDEM(TM) FS-USB and they include unnecessary codes for our simple PIC board.  If you look at the firmware codes in Keyboard.c and find the codes indicated by //YTS, you will understand what I do. As the unnecessary codes still remain, you can remove them if you want to.

*I found later that no modification was needed.

5.
Build the obtained firmware and write it in PIC. After connecting PIC to PC by a USB cable, execute the firmware. Open an editor program (notebook or word etc.) on PC. As sw3 is defined to be PORTBbits.RB5 in "HardwareProfile - PICDEM FSUSB.h", touch Pin#38 of PIC with your finger (you give the pin "noises"). Then characters (abcdef.....) appear on a window of your editor program. For practical use,  Pin #38 should be pulled up to VDD(5V) by firmware (or a resistor). Software pull-up can be achieved by firmware for port B and D in this PIC. (RBPU bit ;INTCON2<7>  for port B and RDPU bit; PORTE<7> for port D. As for the meaning of a pull-up resistor, see Pull-up resistor.

Place 4550_keyboard.zip (modified firmware) and Microchip.zip in the root directory of drive D (d:\).

Add one lien in the function "InitializeSystem()" of Keyboard.c. to pull-up all pins of port B (from RB0 to RB7).Even if you touch pin38 (that is, even if you give some noises to pin38), nothing happens anymore.Only when you connect pin38 to GND, character appears one by one on the window of your editor program.  

static void InitializeSystem(void)
{
    #if (defined(__18CXX) & !defined(PIC18F87J50_PIM))
        ADCON1 |= 0x0F;                 // Default all pins to digital
        INTCON2bits.RBPU=0;                //<------------------------ Add this line to pull-up port B
    #elif defined(__C30__)
        #if defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GB106__)

As for the configuration bits for this PIC, see C:\Program Files (x86)\Microchip\MPASM Suite\P18F4550.INC.
As for the HID key codes, see HID key codes (HID Usage ID).


HID 4x4 Keypad

 RB0~RB3 are for Col1~Col4, and RB4~RB7 for Row1~Row4 (no key interrupt)

Place 4550_KeyPad and Microchip.zip in the root directory of drive D (d:\).


Explanation for the firmware "HID 4x4 Keypad"


A) Structure for sending data

There are two things that we should consider.
1. ProcessIO() is called by main() repeatedly.
2. HIDTxHandleBusy(lastINTransmission) is not always zero.

Code structure for sending data to PC looks like

if(!HIDTxHandleBusy(lastINTransmission))
   {SEND KEY DATA TO PC}
if(!HIDTxHandleBusy(lastINTransmission))
   {SEND RELEASE KEY DATA TO PC}

{SEND KEY DATA TO PC} may be executed, but {SEND RELEASE KEY DATA TO PC} may not be executed at the same time. When Keyboard(void) is called by ProcessIO() next time, only {SEND RELEASE KEY DATA TO PC} should be executed in this case. We should use a flag indicating that key data have been already sent but release key data have not. FlagSentData is the flag.

B) Structure for key scans


first key scan
waiting for 20 msec (500/12 msec , or Delay10KTCYx(50); must be better)
second key scan

When chattering of switch occurs, the data obtained by first key scan are different from those by second key scan. Only when they are the same, we know that a key(s) is(are) really pressed. For example, PB0bit is stored in buf1[0][0] with “buf1[0][0]=PORTBbits.RB0” by the first key scan. After 20(50) msec, we check if the bit stored in buf1[0][0] is the same as a PB0bit obtained by the second key with “if(buf1[0][0]!= PORTBbits.RB0) return”. If not the same, the chattering occurs and we just return to ProcessIO(). PB0bit stored in buf1[0][0] is ignored in this case.

C) Check for key press


A sum of all bits obtained by key scans is calculated as,
    bufsum=0; for(j=0;j<4;j++)for(i=0;i<4;i++) bufsum+=buf1[j][i];

When no key is pressed, bufsum is 16. When only one of 16 keys is pressed, bufsum is 15. When two or more keys are pressed at the same time, bufsum is less than 15. In the last case, we ignore the key scan data and return to ProcessIO() by using a code “if(bufsum<15 ) return”.

Summary;
FlagSentData==1; key data has been sent to PC, but release key data have not sent yet.
FlagSentData==0; no key event has been occurred yet (or, both of key data and release key data have been already sent to PC).
bufsum==15: one key is pressed
bufsum ==16; no key is pressed (or release a key)


D) Sending key data

i) When one key is pressed (bufsum==15) and key data have not been sent yet (FlagSentData==0), the key data must be sent.
ii) When one key is pressed (bufsum==15) and key data have been already sent to PC (FlagSentData==1; user keeps pushing the same key), we should ignore this key event and return to ProcessIO().
iii) No key is pressed (bufsum ==16) and key data have not been sent yet (FlagSentData==0), meaning that no key event has occurred. We should return to ProcessIO().
iv) When no key is pressed (bufsum ==16), and key data have already sent to PC but release key data have not sent yet (FlagSentData==1), the release key data must be sent. Otherwise, data sequence of “aaaaaaaaaaaaa….” will appear on the window of your editor program, even if you push the ‘a’ key and then release it immediately.

E) Additional information on key scan

Beginning of key scans looks like

    //PORTBbits.RB0 Col1
    //PORTBbits.RB1 Col2
    //PORTBbits.RB2 Col3
    //PORTBbits.RB3 Col4
    PORTBbits.RB4 =0; //Row1
    PORTBbits.RB5 =1; //Row2
    PORTBbits.RB6 =1; //Row3
    PORTBbits.RB7 =1; //Row4

    buf1[0][0]=PORTBbits.RB0; buf1[0][1]=PORTBbits.RB1; buf1[0][2]=PORTBbits.RB2; buf1[0][3]=PORTBbits.RB3;

In this case, only RB4bit (Row1) is 0 (L or 0V). When you press a (Col3;Row1) switch, only RB2bit (Col3;) is read to be 0 (L or 0V) and a value 0 is stored in buf1[0][2]. The each of the other buffers (buf1[0][0], buf1[0][1], and buf1[0][3]) has a value of 1, because all input pins (from RB0 to RB3) are pulled up to Vdd by the firmware.


PORTB Interrupt-on-Change

Set a tactile (or momentary) switch between a pin#37 (RB4) and GND (Vss) and observe the voltage of pin#2 (RA0). When you push the switch, the voltage of pin#2 changes from 5 (0) to 0 (5) V.

4550_INT.zip

AD Converter

Two potentiometers (20 k ohm) are used to test AD converter functionality of PIC18F4550. For each potentiometer, one and the other end terminals were connected with GND (Vss) and Vdd, respectively. The middle (contact) terminal was connected with #2 or #3 pin of the PIC. These pins were set to be analog inputs for AD converters. The voltages of these pins have been read (reading values are in the range between 0x0000 and 0x03ff). The following firmware should be executed in the debug mode and a value of data (int variable) should be monitored in a watch window of MPLAB.

4550_AD.zip


IR Transmitter with NEC Protocol

Connect  the following electric circuit to RA0. The current for D1 should be about 100 mA. Change the value of R1 for your system.


A firmware for IR transmitter, where NEC protocol ( http://www.sbprojects.com/knowledge/ir/nec.htm ) was used. It sends the code for “Toshiba TV channel up” repeatedly. It was a little bit difficult to make a precise pulse train (or sequence) by using c language. As command processing overhead time was unknown, I had to make the train by trial and error. As for the IR receiver, see the section of PIC18F2550.

4550_IR.zip

Mouse cursor movement with two potentiometers

Connect  two potentiometers to PIC as described above (see AD converter) and connect PIC to PC with a USB cable. This firmware is utilized to navigate mouse-cursor-position by using an analog joystick.

Place 4550Joy_Mouse.zip and Microchip.zip in the root directory of drive D (d:\).



Multiple Top-Level Collection devise (HID-mouse + HID-keyboard)

Here is a firmware for a multiple top-level collection deviseard.*) Main differences from the firmware “4550_Mouse.zip” are as follows.

1.    Usb_descripters.c
a.    DESC_CONFIG_WORD (115) //215th line from the top
b.    DESC_CONFIG_WORD (9) //222nd line from the top
c.    HID REPORT DESCRIPTOR //247th line from the top

2.    usb_config.h
a.    #define HID_INT_OUT_EP_SIZE 9
b.    #define HID_INT_IN_EP_SIZE 9
c.    #define HID_RPT01_SIZE 115

3.   mouse.c
See the codes in it (indicated by "//YTS").

After executing your editor program (notepad etc.), connect the PIC board to PC with a USB cable. Mouse cursor draws a circle and characters (sequence of “abcd......”) appear in the window of the editor program, alternately (almost simultaneously).


Place 4550_Mouse_Key1.zip  and Microchip.zip in the root directory of drive D (d:\).

*) This is not a composite device using 2 interfaces.

Composite devise (HID-mouse + HID-keyboard)

This firmware totally depends on the following posts.*)
chinzei and MDreamer: http://www.microchip.com/...525&high=composite
Pic_worker34 and chinzei: http://www.microchip.com/...319&high=composite
chinzei: http://www.microchip.com/...=composite&mpage=2


#38 pin is pulled up by the firmware. When you contact this pin to GND, characters would appear (chattering may occur). Mouse cursor draws a circle.

I started from "USB Device - HID - Mouse - C18 - PICDEM FSUSB" to make the firmware. The lines I changed are indicated by //YTS.


Place  4550_Mouse_Key_COM_YTS.zip in the root directory of drive D (d:\).

*) This is a composite device using 2 interfaces.


Composite devise (2 HIDs)

Firmware:
*)

Place 4550_2HID.zip  in the root directory of drive D (d:\)

Application software:

VC6.zip

I made a class named “Interface” by using VC++ Ver. 6. This class can get read- and write-handles for a specific interface number. When “CInterface aaa(0);” is declared, the write-handle for interface #0 is given by aaa.WriteHandle. Similarly, “CInterface bbb(1);” is declared, the write-handle for interface #1 is given by bbb.WriteHandle. Here the name of instance, “aaa” or “bbb”, is arbitrary.

When you execute the application software on PC, a dialog window appears with two edit boxes and two buttons. If you write some characters in the upper edit box and push one of button, the characters appear in the lower edit box. You would notice that only first character is changed. If you type “123” in the upper box and push the INT1 button, “223” will appear in the lower box. First character of ‘2’ = ‘1’ +1 in ASCII code. If you push the INT2 button, “323” will appear. First character of ‘2’ = ‘1’ +2 in ASCII code.

*) This is a composite device using 2 interfaces.


Composite devise (Keyboard + Generic HID) 

Firmware:
*) 

    4550_KeyHid.zip

Application software:

    VC6.zip (see above)


Interfaces #1 and #2 are for keyboard and Generic HID (64 Bytes), respectively.

Keyboard : After executing your editor program (notepad etc.), connect the PIC board to PC with a USB cable and touch #38 pin of PIC18F4550 by a finger. Characters (sequence of “abcd......”) appear in the window of the editor program.


Generic HID :  When you execute the application software  (VC6.exe) on PC, a dialog window appears with two edit boxes and two buttons.  If you type “123” in the upper box and push the INT2button (DON’T push the INT1 button), “323” will appear in the lower box. First character of ‘3’ is equal to‘1’ +2 in ASCII code. 


*) This is a composite device using 2 interfaces.


IR data transfer from IR Transmitter to IR Receiver. The data are of mouse and key.
The data transfer speed is quite slow, because I use NEC Protocol.

IR Transmitter with modified NEC Protocol

Make the electric circuit by following  "IR Transmitter with NEC Protocol" in PIC18F4550 section.
When RB3 (RB2) contacts  with GND, the data for mouse (keyboard) is sent to IR Receiver (see below) via IR. If you have  an oscilloscope, please observe the output of RA0 (#2 pin).


Place 4550_Mouse_Key_TX.zip and Microchip.zip in the root directory of drive D (d:\).

The output signal of RA0 is observed as shown in IR1.pdf

Modified NEC Protocol is as follows;

LEADER + ID (1Byte) + DATA(4Bytes) + STOP.

Here, ID is 'M' for mouse, and 'K' for keypad.

IR Receiver with modified NEC Protocol

Make the electric circuit by following "IR receiver with NEC Protocol" in PIC18F2550 section.
Connect the circuit with PC by a USB cable.
When RB3 (#36 pin)  contacts with GND on the transmitter side (PIC18F4550: see just above), the mouse cursor moves to the right by four steps.   When  RB2 (#35 pin) contacts with GND on the transmitter side (PIC18F4550), the characters  "K + space"  appear on your window of notepad. This firmware realizes a multiple TLC (Top-Level Collection). Though I have used PIC18F2550 here, the firmware can be used for PIC18F4550 too.


Place  2550_Mouse_Key_RX.zip  and Microchip.zip in the root directory of drive D (d:\).



private section:


2550_Mouse_Key_RX_INT.zip
Comments