Showing posts with label PIC. Show all posts
Showing posts with label PIC. Show all posts

Monday, January 13, 2014

Mini Automation Project

In this project, you can config two relays to start/stop high power devices on a specific time using PIC18f4550 and PCF8583.
Here is the Proteus Simulation and CCS Project.
All setting are configurable. It has simple 3 level menu by which you can edit start time and stop time of each relay and also you can change the current time. Since PCF8583 is used and every settings is saved on PIC18f4550 memory, even when electricity run off this device can operate normally afterward.
Here is Proteus Schematic:



Here is the actual Device:



Friday, November 30, 2012

7 Segment increment by switch PIC18f4550

#include <18f4550 .h=".h">
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
//#fuses HS, NOWDT
#use delay(clock=20MHz)


static int8 const led_table[20] =  
//      ----           seg A
//     |    |   seg F         seg B
//     |    |
//      ----           seg G
//     |    |   seg E         seg C
//     |    |
//      ----           seg D
//
// bit7 .. bit0  ==>>   seg  None,G,A,F,D,E,C,B

// TI Driver connections:
// OUT0 -> None
// OUT1 -> Seg G
// OUT2 -> Seg A
// OUT3 -> Seg F
// OUT4 -> Seg D
// OUT5 -> Seg E
// OUT6 -> Seg C
// OUT7 -> Seg B

{
0x40,    // 0
0x79,    // 1
0x24,    // 2
0x30,    // 3
0x19,    // 4
0x12,    // 5
0x02,    // 6
0x78,    // 7
0x00,    // 8
0x10,    // 9
0x08,    // A
0x03,    // b
0x46,    // C
0x21,    // d
0x06,    // E
0x0e,    // F
};



void main() {
int8 i=0;
while(1){
if( input(PIN_A5) ){
delay_ms(500);
if( input(PIN_A5) ){
if (i>15)
      i=0;
   SET_TRIS_D(led_table[i]);
   i=i+1;
}  
}
}
}

Seven-segment display PIC18f4550

#include <18f4550 .h=".h">
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
//#fuses HS, NOWDT
#use delay(clock=20MHz)


static int8 const led_table[20] =  
//      ----           seg A
//     |    |   seg F         seg B
//     |    |
//      ----           seg G
//     |    |   seg E         seg C
//     |    |
//      ----           seg D
//
// bit7 .. bit0  ==>>   seg  None,G,A,F,D,E,C,B

// TI Driver connections:
// OUT0 -> None
// OUT1 -> Seg G
// OUT2 -> Seg A
// OUT3 -> Seg F
// OUT4 -> Seg D
// OUT5 -> Seg E
// OUT6 -> Seg C
// OUT7 -> Seg B

{
0x40,    // 0
0x79,    // 1
0x24,    // 2
0x30,    // 3
0x19,    // 4
0x12,    // 5
0x02,    // 6
0x78,    // 7
0x00,    // 8
0x10,    // 9
0x08,    // A
0x03,    // b
0x46,    // C
0x21,    // d
0x06,    // E
0x0e,    // F
};



void main() {

 
while(1){
SET_TRIS_B(led_table[0]);
 delay_ms(500);
 SET_TRIS_B(led_table[1]);
 delay_ms(500);
 SET_TRIS_B(led_table[2]);
 delay_ms(500);
 SET_TRIS_B(led_table[3]);
 delay_ms(500);
 SET_TRIS_B(led_table[4]);
 delay_ms(500);
 SET_TRIS_B(led_table[5]);
 delay_ms(500);
 SET_TRIS_B(led_table[6]);
 delay_ms(500);
 SET_TRIS_B(led_table[7]);
 delay_ms(500);
 SET_TRIS_B(led_table[8]);
 delay_ms(500);
 SET_TRIS_B(led_table[9]);
 delay_ms(500);
 SET_TRIS_B(led_table[10]);
 delay_ms(500);
 SET_TRIS_B(led_table[11]);
 delay_ms(500);
 SET_TRIS_B(led_table[12]);
 delay_ms(500);
 SET_TRIS_B(led_table[13]);
 delay_ms(500);
 SET_TRIS_B(led_table[14]);
 delay_ms(500);
 SET_TRIS_B(led_table[15]);
 delay_ms(500);
}
// your program here
}

PCF8583 Real-time clock (RTC) CCS program for PIC18f4550

Download the code here

Wednesday, November 28, 2012

serial communication using PIC18f4550

//CCS C compiler
#include <18f4550 .h=".h">
#fuses HS,NOWDT
#use delay(clock=20MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_B1,rcv=PIN_B2,bits=8,STOP=1)

void main()
{

while(TRUE)
{
printf("mahyar");
}
} 


Monday, March 8, 2010

PWM speed control (pic18f4550)

here is the ccs code:

#include <18f4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 20000000)
void main() {
unsigned int16 value;// Input variable
setup_adc_ports(ALL_ANALOG);// Input combination
setup_adc(adc_clock_internal);// ADC clock
set_adc_channel(0);// Select RA0
setup_ccp1(ccp_pwm); // Select timer and mode
setup_timer_2(T2_DIV_BY_16,248,1); // Clock rate & output period
while(1) {
value=read_adc();//Get input byte
set_pwm1_duty(value);// Set on time
}
}

everything is exactly same as previous post + a simple potentiometer which is connected to pin2.

here is the execution:

Driving a motor (pic18f4550)

you can use the information mentioned in previous post as the pic side program to increase or decrease the speed of a motor as an example. now I want to use ULN2003APG driver to drive a motor.

using ULN2003APG :

pin1 ---> MCU output
pin16 ---> Motor
pin8 ---> Motor GND and MCU GND

and connect vcc to the motor.we use mcu signal to connect gnd to the motor to run with separate vcc but common gnd.


PWM (pic18f4550)

to use pwm here is the ccs code:

#include <18f4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock = 20000000)

void main() {
setup_ccp1(ccp_pwm); // Select timer and mode
set_pwm1_duty(25); // Set on time
setup_timer_2(T2_DIV_BY_16,248,1); // Clock rate & output period
while(1) { } // Wait until reset
}

pin17 ---> pwm output
+ everything was mentioned in previous posts in order to run a 18f pic.

Tuesday, March 2, 2010

Hid Demo

finally I could find a library in order to work with usb very easily here is the library itself.
usb hid can be downloaded here which is written in c#.
usb pic side can be downloaded here which is made which ccs.

this code is not complete and is just for learning how to use usbLibrary. In order to use see the result :
pin2 ---> ADC
pin20 ---> led+resistor(470 ohm)
+ everything was mentioned in previous post in order to implement usb.
and this is the hid:

Saturday, February 27, 2010

hiddemo.exe and OScope.exe

this file is compatible with the example ex_usb_hid.c which is mentioned in ccs:
CCS DELPHI HOST DEMO

USB test with pic18f4550




just program .hex file to the chip the code was written in mcc18 :
Generic HID pic side
Generic HID pc side

USB

+5V
Data +
DATA -
GND

Friday, February 26, 2010

Lcd Hello World pic18f4550



here is ccs code :

#include <18f4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)
#include

void main() {
lcd_init();
lcd_putc("Hello!");
}

LCD pins D0-D3 are not used and PIC D3 is not used.

D0 enable
D1 rs
D2 rw
D4 D4
D5 D5
D6 D6
D7 D7

strange errors in ProgICD2

I prefer using mplab programmer itself but inorder to program pic 16f84 there is no other choice.

LED Blink pic18f4550

here is the code written in ccs :

#include <18f4550.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

void main() {

while (TRUE) {
output_high(PIN_D1);
delay_ms(500);
output_low(PIN_D1);
delay_ms(500);
}
}

remeber to select PCH 16 bit to compile

pin1 ---> vcc
pin11 ---> vcc
pin12 ---> gnd
pin13 & pin14 ---> crystal + 20pf capacitor
pin20 ---> led+resistor(470 ohm)
pin31 ---> gnd
pin32 ---> vcc

Thursday, February 25, 2010

Lcd Hello World pic16f84a






here is the mikroC code:

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char *text = "Hello World"; //Constant holding text to be displayed

void main() //Main program
{
TRISB = 0; // PORTB is output
Lcd_Init(); // Initialize LCD connected to PORTB
Lcd_Cmd(_Lcd_CLEAR); // Clear data on lcd
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
while(1) //while loop, not necessarily needed
{
PORTA = ~PORTA;
Lcd_Out(1, 1, text); // Print text to LCD, 2nd row, 1st column
}
}

datasheet

pic18f4550
pic16f84
ULN2003APG
LCD 2x8

LED Blink pic16f84a

here is the mikroC code :

void main() {
PORTB = 0;
TRISB = 0;
while (1) {
PORTB = ~PORTB;
Delay_ms(100);
}
}

and

pin4 ---> vcc
pin5 ---> gnd
pin9 ---> led+resistor(470 ohm)
pin14 --->vcc
pin15 & pin16 ---> crystal + 20pf capacitor