SK40C Beginner

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Re: SK40C Beginner

Postby ABSF » Mon Mar 19, 2012 10:49 pm

Vicky WROTE:how can i program so that 16F887 can do two task at the same time?
such as i want LED1 and LED2 at sk40c blinking,
while LED1-LED6 on another circuit do chase blink.
anyone can help?
i think it is about interrupt, isn't it?
this is my code.
i fail to do it.


To do the above job you need to set up a timer to generate an interrupt every 50 mS. I am not good in explaining the concept of how it works. SO it would be better if you get the information elsewhere on the net :mrgreen:

To learn how interrupt works on PIC, you can google using "Interrupt on PIC" on the net and find out how other people write programs using interrupt. You can also read the datasheets on the chapter on "Interrupt" on older chips like 16F628. I found out that newer datasheets had taken out the chapter on this subject and spread them out bit by bit on other chapters.

You can also go to main microchip website and search for Application Notes that describe how Interrupt was used. After you understand about Interrupt, then you can try to write your own program to do 2 tasks at the same time... :mrgreen:

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: SK40C Beginner

Postby Brian Griffin » Mon Mar 19, 2012 10:56 pm

I may sound like a broken record, but the book "PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18" by Mazidi is a very good read.

Plus, Myke Predko's "123 PIC Microcontroller Projects for the Evil Genius" is very helpful too.

If your campus library has one of these, borrow them. :)
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: SK40C Beginner

Postby vick5821 » Mon Mar 19, 2012 11:03 pm

Brian Griffin WROTE:I may sound like a broken record, but the book "PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18" by Mazidi is a very good read.

Plus, Myke Predko's "123 PIC Microcontroller Projects for the Evil Genius" is very helpful too.

If your campus library has one of these, borrow them. :)

For PIC18 ? Can be apply in PIC 16?
vick5821
Discoverer
 
Posts: 85
Joined: Tue Jun 28, 2011 10:21 pm

Re: SK40C Beginner

Postby Brian Griffin » Mon Mar 19, 2012 11:14 pm

vick5821 WROTE:
Brian Griffin WROTE:I may sound like a broken record, but the book "PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18" by Mazidi is a very good read.

Plus, Myke Predko's "123 PIC Microcontroller Projects for the Evil Genius" is very helpful too.

If your campus library has one of these, borrow them. :)

For PIC18 ? Can be apply in PIC 16?


The first book is for the PIC18, but rest of the theory can be applied to PIC16 as well.

The second book is entirely for the PIC16F series.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: SK40C Beginner

Postby vick5821 » Mon Mar 19, 2012 11:20 pm

Brian Griffin WROTE:
vick5821 WROTE:
Brian Griffin WROTE:I may sound like a broken record, but the book "PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18" by Mazidi is a very good read.

Plus, Myke Predko's "123 PIC Microcontroller Projects for the Evil Genius" is very helpful too.

If your campus library has one of these, borrow them. :)

For PIC18 ? Can be apply in PIC 16?


The first book is for the PIC18, but rest of the theory can be applied to PIC16 as well.

The second book is entirely for the PIC16F series.

Oh..my library just have the first book..lol

Thank you
vick5821
Discoverer
 
Posts: 85
Joined: Tue Jun 28, 2011 10:21 pm

Re: SK40C Beginner

Postby Vicky » Mon Apr 02, 2012 3:33 pm

guys.. please help me..
my project is about mailbox..
this mailbox has keypad security, only postmen and user can open the mailbox..
what i want to do is when the postmen enter his postmen password, LED1 start blinking,
until the user enter his user password, LED1 stop blinking.
This is my coding so far...
until now, i cant figure how to make the led blinking if the postmen put letter, blinking until user take the letter
the LED1 is for notification, whether got letter in mail box or not

coding
CODE: SELECT_ALL_CODE
//==========================================================================
//   Author            : CYTRON TECHNOLOGIES SDN BHD
//   Project            : SK40C sample code for PIC16F887
//   Project description   : Driving DC brush motor with MD10C
//                    2 mode: (1: Sign-Magnitude PWM)
//                          (2: Locked-Antiphase PWM)
//==========================================================================

//   include
//==========================================================================
#include <htc.h>
#include "lcd.h"
#include "system.h"
#include "pwm.h"

//   Configuration
//==========================================================================
__CONFIG(FOSC_HS &         // External Crystal at High Speed
       WDTE_OFF &      // Disable Watchdog Timer.
       PWRTE_ON &      // Enable Power Up Timer.
       BOREN_OFF &      // Disable Brown Out Reset.
       MCLRE_ON &      // MCLR function is enabled
       LVP_OFF);      // Disable Low Voltage Programming.

//   function prototype
//==========================================================================
void delay_ms(unsigned int ui_value);
void sign_magnitude_pwm(void);
void locked_antip_pwm(void);
void wait_sw1(void);
void delay_ms(unsigned int ui_value);
void clearrow1(void);
void scancolumn1();
void clearrow2(void);
void scancolumn2(void);
void clearrow3(void);
void scancolumn3(void);
void clearrow4(void);
void scancolumn4(void);

unsigned char password_count=0;
unsigned char keyin_char[6];            // Declare an array to stall the 6-digit key in password
unsigned char stalled_char[6]="111111";
unsigned char stalleduser_char[6]="222222";

//   main function
//==========================================================================
void main(void)
{   
   PORTA = 0;               
   PORTB = 0;                     // Clear Port
   PORTC = 0;
   PORTD = 0;

   TRISA = 0b00001111;               // set PORTA as OUTPUT
   TRISB = 0b00000011;               // set PORTB<7:3> as OUTPUT , PORTB<2:0> as INPUT
   TRISC = 0b00000000;               // set PORTC as OUTPUT
   TRISD = 0b00000000;               // set PORTD as OUTPUT
   TRISE = 0b00000000;   

   ANSEL = 0;                     // Set PORTA as Digital I/O
   ANSELH = 0;                   // SET PORTB as DIGITAL I/O for PIC16F887

   lcd_initialize();               // Initialise LCD
   pwm_initialize();               // Initialise PWM1
   
   lcd_clear();
   lcd_home();                      // LCD return cursur to home position which is 0x00.
   lcd_putstr("Please enter 6\ndigit password ");      //LCD display (Please refer lcd.c for detail)
   delay_ms(100);
   PWM=1;
   

   while(1)
   {   
      clearrow1();                  //function for scanning keypad
      scancolumn1();
      clearrow2();
      scancolumn2();
      clearrow3();
      scancolumn3();
      clearrow4();
      scancolumn4();

      if (password_count==6)            //if the 6 digit entered through keypad, start comparing password
      {
         password_count==0;            //if the password for the postmen is true, then the gate for the mailbox is open
         if((keyin_char[0]==stalled_char[0])&&(keyin_char[1]==stalled_char[1])&&(keyin_char[2]==stalled_char[2])
            &&(keyin_char[3]==stalled_char[3])&&(keyin_char[4]==stalled_char[4])&&(keyin_char[5]==stalled_char[5]))
         {
            lcd_clear();
            lcd_goto(0);
            lcd_putstr("Identification:\n Postmen");        //command to open the hole to insert letter
            LED2=1;
            DIR=0;
            pwm_set_duty_cycle(500);                  //hole for letter is open
            delay_ms(600);
            pwm_set_duty_cycle(0);                     //delay it
            delay_ms(2000);
            DIR=1;
            pwm_set_duty_cycle(1021);                  //close the hole
            delay_ms(300);
            pwm_set_duty_cycle(0);
            
      ///////////---from here, i want to make LED1 blinking to notify user, got letter in mailbox----//////////////////
            break;
         }


         else if((keyin_char[0]==stalleduser_char[0])&&(keyin_char[1]==stalleduser_char[1])&&(keyin_char[2]==stalleduser_char[2])
            &&(keyin_char[3]==stalleduser_char[3])&&(keyin_char[4]==stalleduser_char[4])&&(keyin_char[5]==stalleduser_char[5]))
         {
            lcd_clear();                              //if the password for the user is correct,start the comand
            lcd_goto(0);
            lcd_putstr("Identification:\n User 001");         //command to open the hole to take the letter
            LED1=1;
            DIR=1;
            pwm_set_duty_cycle(1021);                     //open the hole
            delay_ms(900);
            pwm_set_duty_cycle(0);                        //delay
            delay_ms(2000);
            DIR=0;
            pwm_set_duty_cycle(500);                     //close the hole
            delay_ms(500);

       ////////---from here, i want to make LED1 stop blinking, to indicate that no letter in mailbox, as user has taken the letter----//////////
            break;
         }         

         else                                       //if the password is wrongly put, display wrong password.
         {
            lcd_clear();
            lcd_goto(0);
            lcd_putstr("Warning!!\nWrong Password!!");
            delay_ms(3000);
            break;
            ///////----this condition will never change the command of the LED1, whether LED1 is blinking or not---///////
         }
      }      


   }

   
}

/*******************************************************************************
* PRIVATE FUNCTION: sign_magnitude_pwm
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* MD10C Sign-Magnitude PWM mode
*
*******************************************************************************/
void sign_magnitude_pwm (void)
{
unsigned int speed;   
lcd_clear();
lcd_home();
lcd_putstr("DIR:");
lcd_goto(0x07);
lcd_putstr("PWM:");
lcd_2ndline();
lcd_putstr("Sign-Mag PWM    ");
      
      lcd_goto(0x04);
      lcd_putstr("1");
      DIR = 1;
      for (speed=350 ; speed<1024 ; speed++)
         {
         delay_ms(10);
         pwm_set_duty_cycle(speed);
         lcd_goto(0xB);
         lcd_bcd(4,speed);
         }

      lcd_goto(0x04);
      lcd_putstr("0");
      DIR = 0;
      for (; speed>350 ; speed--)
         {
         delay_ms(10);
         pwm_set_duty_cycle(speed);
         lcd_goto(0xB);
         lcd_bcd(4,speed);
         }      
}
      
/*******************************************************************************
* PRIVATE FUNCTION: locked_antip_pwm
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* MD10C Locked Anti-Phase PWM mode
*
*******************************************************************************/
void locked_antip_pwm (void)
{                              
unsigned int speed;   
lcd_clear();
lcd_home();
lcd_putstr("DIR:");
lcd_goto(0x07);
lcd_putstr("PWM:");
lcd_2ndline();
lcd_putstr("Locked-Antip PWM");
      

      lcd_goto(0x04);
      lcd_putstr("1");
      DIR = 1;
      for (speed=1024 ; speed>0 ; speed--)
         {
         delay_ms(10);
         pwm_set_duty_cycle(speed);
         lcd_goto(0xB);
         lcd_bcd(4,speed);
         }
}
   

/*******************************************************************************
* PRIVATE FUNCTION: delay_ms
*
* PARAMETERS:
* ~ ui_value   - The period for the delay in miliseconds.
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Delay in miliseconds.
*
*******************************************************************************/
void delay_ms(unsigned int ui_value)
{
   while (ui_value-- > 0) {
      __delay_ms(1);      // macro from HI-TECH compiler which will generate 1ms delay base on value of _XTAL_FREQ in system.h
   }   
}

/*******************************************************************************
* PRIVATE FUNCTION: wait_sw1
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* wait for switch 1 to be press while have delay
*
*******************************************************************************/
void wait_sw1(void)
{
   unsigned char i;
   for (i = 0; i < 200; i++)
      {
         if (SW1 == 0)
         {
            return;
         }   
         delay_ms(10);
      }
}

void clearrow1(void)         //clear the 1st row and set the others
{
   R1=0;                  //RE1,RE0, RA5 and RA4 are the output pins from PIC which connect to 4 pins of keypad      
   R2=1;
   R3=1;
   R4=1;            
}

void clearrow2(void)         //clear the 1st row and set the others
{
   R1=1;                  //RE1,RE0, RA5 and RA4 are the output pins from PIC which connect to 4 pins of keypad      
   R2=0;
   R3=1;
   R4=1;            
}

void clearrow3(void)         //clear the 1st row and set the others
{
   R1=1;                  //RE1,RE0, RA5 and RA4 are the output pins from PIC which connect to 4 pins of keypad      
   R2=1;
   R3=0;
   R4=1;            
}

void clearrow4(void)         //clear the 1st row and set the others
{
   R1=1;                  //RE1,RE0, RA5 and RA4 are the output pins from PIC which connect to 4 pins of keypad      
   R2=1;
   R3=1;
   R4=0;            
}

void scancolumn1(void)
{
   if(C1==0)
   {
      while(C1==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='1';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }
   
   if(C2==0)
   {
      while(C2==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='2';   //Stall the '1' value at the keyin_char array
      password_count+=1;

   }

   if(C3==0)
   {
      while(C3==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='3';   //Stall the '1' value at the keyin_char array
      password_count+=1;

   }

   /*if(C4==0)
   {
      while(C4==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='A';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   
   }*/

}

void scancolumn2(void)
{
   if(C1==0)
   {
      while(C1==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='4';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }
   
   if(C2==0)
   {
      while(C2==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='5';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }

   if(C3==0)
   {
      while(C3==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='6';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }

   /*if(C4==0)
   {
      while(C4==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='B';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }*/

}

void scancolumn3(void)
{
   if(C1==0)
   {
      while(C1==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='7';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }
   
   else if(C2==0)
   {
      while(C2==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='8';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }

   if(C3==0)
   {
      while(C3==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='9';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }

   /*if(C4==0)
   {
      while(C4==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='C';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }*/

}

void scancolumn4(void)
{
   if(C1==0)
   {
      while(C1==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='*';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }
   
   if(C2==0)
   {
      while(C2==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='0';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }

   if(C3==0)
   {
      while(C3==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='#';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }

   /*if(C4==0)
   {
      while(C4==0)continue;         //waiting the key to be released
      if(password_count==0)
      {lcd_clear();}   //Clear the LCD if the key is the 1st password
      lcd_goto(password_count);      //The cursor of LCD points to the column equivalent to the value of password_count variable
      lcd_putstr("*");               //Display the symbol '*' at LCD   
      keyin_char[password_count]='D';   //Stall the '1' value at the keyin_char array
      password_count+=1;
   }*/

}


coding for system.h
CODE: SELECT_ALL_CODE
/*******************************************************************************
* This is the header file that describes the system properties for PTK40A
*
* Cytron Technologies Sdn. Bhd.
*******************************************************************************/
#ifndef _SYSTEM_H
#define _SYSTEM_H

//   define
//==========================================================================
#define   _XTAL_FREQ   20000000   /* Crystal frequency in MHz */

#define SW1    RB0   
#define SW2      RB1                     

#define LED1   RB6
#define LED2   RB7

///keypad/////
#define C1  RA0
#define   C2   RA1
#define C3   RA2
#define C4   RA3
#define R1   RA4
#define R2   RA5
#define   R3   RE0
#define R4   RE1

// I/O Connections.
// Parallel 2x16 Character LCD
#define LCD_E         RB5      // E clock pin is connected to RA3   
#define LCD_RS         RB4      // RS pin is used for LCD to differentiate data is command or character, RA2

#define   LCD_DATA      PORTD   // Data port of LCD is connected to PORTD, 8 bit mode
                        // DB7(LCD) = RD7(PIC)
                        // DB6(LCD) = RD6(PIC)
                        // DB5(LCD) = RD5(PIC)
                        // DB4(LCD) = RD4(PIC)
                        // DB3(LCD) = RD3(PIC)
                        // DB2(LCD) = RD2(PIC)
                        // DB1(LCD) = RD1(PIC)
                        // DB0(LCD) = RD0(PIC)

// UART
#define TX      RC6
#define RX      RC7

// External component define here
//=================================================================================
//MD10C I/O
#define PWM         RC1
#define DIR         RC2

#endif
Vicky
Newbie
 
Posts: 8
Joined: Fri Mar 02, 2012 6:24 pm

Re: SK40C Beginner

Postby kl84 » Mon Apr 02, 2012 9:16 pm

Is there any reason why you use Cytron's motor control sample code for your mailbox project? Since you want to blink an LED, why not start writing from LED blinking sample code? They have one DIY project called PR1 if not mistaken...
User avatar
kl84
Amateur
 
Posts: 166
Joined: Thu Jul 08, 2010 12:14 pm

Re: SK40C Beginner

Postby robosang » Thu Apr 05, 2012 11:08 am

Agreed with kl84, I always have doubt about your project. Frankly, this thread is quite messy.... jump here, jump there.

You want to try LED blinking, but you post a complex example code from Cytron for motor driver? What is that have to do with LED blinking except it is a code for PIC? Just take the code from SK40C page that you have been using and modify it. Is not possible to ask people to check the code when you are not taking effort to even look at it and modify it.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Previous

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 0 guests

cron