SK40C Beginner

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

Re: SK40C Beginner

Postby vick5821 » Fri Mar 09, 2012 5:57 pm

robosang WROTE:system.h is just a header file like htc.h. Why must be system.h? No special reason except you write some code in that file and you need it in your main file. As allen pointed out, if you don like to include system.h you can write all the needed code on your main file.


BTW, "system" is just a name given to a file, is a file name, you want to write your name as the file name also no problem, but you need to let your main file know where is the file? Must through the correct file name.

So I create the file externally ? It is another MPLAB code file save with the name ?
vick5821
Discoverer
 
Posts: 85
Joined: Tue Jun 28, 2011 10:21 pm

Re: SK40C Beginner

Postby ABSF » Sat Mar 10, 2012 6:02 am

vick5821 WROTE:So I create the file externally ? It is another MPLAB code file save with the name ?


Can you make your question clearer?
What do you mean by "externally"?
What is "MPLAB code file" in your mind? Give some examples.....
What "name" are you refering to?

I really very blur blur blur blur blur blur blur lah! :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 vick5821 » Sat Mar 10, 2012 10:12 am

ABSF WROTE:
vick5821 WROTE:So I create the file externally ? It is another MPLAB code file save with the name ?


Can you make your question clearer?
What do you mean by "externally"?
What is "MPLAB code file" in your mind? Give some examples.....
What "name" are you refering to?

I really very blur blur blur blur blur blur blur lah! :mrgreen:

Allen


How to make a header file ?

I am referring to system.h..Is this the header that we create ourself ?
vick5821
Discoverer
 
Posts: 85
Joined: Tue Jun 28, 2011 10:21 pm

Re: SK40C Beginner

Postby Vicky » Sat Mar 17, 2012 2:43 pm

hi.. i'm new here..
can anyone share simple code,
if sw1 pushed, led1 light up.

i try countless time on the sk40c, still cant figure out..
tq
Vicky
Newbie
 
Posts: 8
Joined: Fri Mar 02, 2012 6:24 pm

Re: SK40C Beginner

Postby vick5821 » Sat Mar 17, 2012 2:56 pm

Vicky WROTE:hi.. i'm new here..
can anyone share simple code,
if sw1 pushed, led1 light up.

i try countless time on the sk40c, still cant figure out..
tq

Refer to tutorial section of cytron website
vick5821
Discoverer
 
Posts: 85
Joined: Tue Jun 28, 2011 10:21 pm

Re: SK40C Beginner

Postby Vicky » Sat Mar 17, 2012 3:09 pm

i have refer to the tutorial for project 0 (led blinking)
there no sw1 or sw2 coding example..
can anyone give me example coding for the switch using led1 and led2 as output?
Vicky
Newbie
 
Posts: 8
Joined: Fri Mar 02, 2012 6:24 pm

Re: SK40C Beginner

Postby ABSF » Sat Mar 17, 2012 4:34 pm

Show your code here so someone can correct it for you.

CODE: SELECT_ALL_CODE
#include <pic.h>

__CONFIG ( 0x3F32 );      //configuration for the  microcontroller

#define   SW1         RB0         
#define   SW2         RB1         
#define   LED1      RB6         
#define   LED2      RB7            

void main()
{
   unsigned long delay_time=3000;
   //set I/O input output
   TRISB = 0b00000011;            //configure PORTB I/O direction
   ADCON1 = 0b00000110;            //set ADx pin digital I/O
   LED1=0;                        //OFF LED1
   LED2=0;                        //OFF LED2

   while(1)                     //Infinite loop
   {
      if(!SW1)                  //check if SW1 is pressed
      {
         while (!SW1);            //wait SW1 pressed
      {
              LED1 = 1;            //switch on LED1
         LED2 = 0;
              delay(delay_time);            
      }
      else if(!SW2)               //check if SW2 is pressed
      {
         while (!SW2);            //wait SW2 pressed
      {
      LED2 = 1;               //switch on LED2         
      LED1 = 0;
                delay(delay_time);         
      }
   }
}

//   functions
//==========================================================================
void delay(unsigned long data)         //delay function, the delay time
{                                 //depend on the given value
for( ;data>0;data--);
}


I modify the above codes from the sample code that was included in the SK40C product page. Not sure if there's any mistake.
Just give it a shot.

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 Vicky » Sat Mar 17, 2012 7:09 pm

tq allen for the code..
i have modified your code a bit..
if the sw1 is pushed, the led1=1 as long the switch is hold,
i able to compile it without coding error.
but still the led wont light up if i press the switch..
this is the coding.
where is the fault?
CODE: SELECT_ALL_CODE
#include <pic.h>

__CONFIG ( 0x3F32 );      //configuration for the  microcontroller

#define   SW1         RB0         
#define   SW2         RB1         
#define   LED1      RB6         
#define   LED2      RB7           

void main()
{
   //unsigned long delay_time = 3000;  //set I/O input output
   TRISB = 0b00000011;               //configure PORTB I/O direction
   ADCON1 = 0b00000110;              //set ADx pin digital I/O
   LED1=0;                           //OFF LED1
   LED2=0;                           //OFF LED2

   while(1)                     //Infinite loop
   {
      if(!SW1)                  //check if SW1 is pressed
      {
        while (!SW1)            //wait SW1 pressed
         {
         LED1 = 1;            //switch on LED1
         LED2 = 0;         
         }
     }

      else if(!SW2)               //check if SW2 is pressed
      {
         while (!SW2);            //wait SW2 pressed
      {
      LED2 = 1;               //switch on LED2         
      LED1 = 0;         
      }
      }
   }
}

//   functions
//==========================================================================
void delay(unsigned long data)         //delay function, the delay time
{                                 //depend on the given value
for( ;data>0;data--);
}
Vicky
Newbie
 
Posts: 8
Joined: Fri Mar 02, 2012 6:24 pm

Re: SK40C Beginner

Postby ABSF » Sun Mar 18, 2012 12:11 am

Your code should work if you remove the ";" on this lines....

         while (!SW2);            //wait SW2 pressed


LED1 should be lighted when SW1 is pressed and will remain lighted when the switch is released. When SW2 is pressed LED2 would light and will also remain lighted until SW1 is pressed.

Try put back the ";" on both lines "while (!SW1);" and "while (!SW2);" and see what is the difference.

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 Vicky » Sun Mar 18, 2012 4:58 pm

owh i have figured out where my fault is..
i forgot to define the sw1 as input at TRISB..
haha..

1 more thing..
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.

CODE: SELECT_ALL_CODE
//==========================================================================
//   Author            : CYTRON TECHNOLOGIES SDN BHD
//   Project            : SK40C sample code for PIC16F887
//   Project description      : Blink LED1 and LED2 like police car
//
//==========================================================================

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

//   Configuration
//==========================================================================
__CONFIG(
   FOSC_HS &    // High Speed Crystal.
   WDTE_OFF &    // Disable Watchdog Timer.
   PWRTE_ON &    // Enable Power Up Timer.
   BOREN_OFF &    // Disable Brown Out Reset.
   MCLRE_ON & // Enable MCLR
   LVP_OFF
      );    // Disable Low Voltage Programming.
//   function prototype
//==========================================================================
void delay_ms(unsigned int ui_value);
void LE(unsigned int light);
//   main function
//==========================================================================
void main(void)
{
   PORTA = 0;            // Clear PORT
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;
   PORTE = 0;   


   TRISA = 0b00000000;      // set PORTA AS output
   TRISB = 0b00000011;    // set PORTB AS output
   TRISC = 0b00001000;      // set PORTC AS output
   TRISD = 0b00000000;      // set PORTD AS output
   TRISE = 0b0001;   

   ANSEL = 0;            // set PORTA as Digital I/O for PIC16F887
   ANSELH = 0;            // set PORTB as Digital I/O for PIC16F887

//LED1,LED2 blinking
   LED1=1;
   delay_ms(50);
   LED1=0;
   LED2=1;
   delay_ms(50);
   LED2=0;

if(SEN1)//sensor1 light up
{   
   LE(1);//led1-led6 at circuit chase blink
}





}

/*******************************************************************************
* 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
   }   
}

void LE(unsigned int light) //led1-led6 function
{
   LE1=light;
   delay_ms(1000);
   LE1=0;
   LE2=light;
   delay_ms(1000);
   LE2=0;
   LE3=light;
   delay_ms(1000);
   LE3=0;
   LE4=light;
   delay_ms(1000);
   LE4=0;
   LE5=light;
   delay_ms(1000);
   LE5=0;
   LE6=light;
   delay_ms(1000);
   LE6=0;
}

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

PreviousNext

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 1 guest