Monday, June 24, 2013

Microcontrollers

I suppose that it is maybe inevitable that I return to digital at some point.  I had purchased a few microcontrollers a while ago but I had not made any use of them.  Microcontrollers are chips that can be programmed by the user and will execute the user defined code when powered on.  At first I was looking at Arduinos.  They seem to be popular with hobbyists.  But I wanted the option of selling a device at some point and using an Arduino would add $50 to the cost of the product.  That makes it too expensive.  The PICs are a popular style of microcontroller that you can buy for as little as $0.49.  I selected the PIC12F675 which has 2K of flash memory because I wasn't sure how much memory I would need.  These are a little over $1.00 each.

I let them sit for about a year.  Part of the reason is I had not used them is that I had purchased a PICKit 2 after reading reviews on the web.  The PICKit is required to program a microcontroller.  First of all the software on the disk they sent with it was corrupted.  That turned out not to be a problem since you can download it from Microchip's web site.  Next I was having trouble with my computers and had to install Ubuntu Linux on one of them.  Then when I did try to program something the lights would flash and it would lock up.  It also took me a while to find some tutorials at the appropriate level for me.

I finally got these to work last weekend.  One of the keys was the system was not detecting the boards.  After I had reseated the programmer and chip a few times it suddenly started working.  The contacts on these must be poor.

Last week I was teaching a class on STEM based on RC race cars.  I thought it would be good to have the students make a circuit that was related to racing, so I thought a red, yellow, green symbol similar to drag racing would be good.  My initial circuit design was all analog.  This started to get bigger and bigger and I finally thought I would investigate the microcontrollers again.

I always like to start small when I learn something new so the first step was to get an LED to turn on.  It turns out that the PICKit 2 doesn't turn on the LEDs with 8 pin chips but you can run a jumper from the output over to the LED.  Also it turns out that GP3 is input only so you can't use that to turn on an LED.  The chip refused to work with my capacitor based rail splitter and a 9V battery so I ended up having to use a voltage regulator to deliver 5 V to the chip.  Some programming later and it started turning on the LED and shortly afterwards I had a simple stop light circuit (see figure).  This should enable me to press a button, have the lights go through a sequence, then hold on green until I press a button again.

For musical applications these could be used to trigger gates for devices to turn them on.  I should be fairly straightforward to implement.

#if defined(__XC)
    #include <xc.h>         /* XC8 General Include File */
#elif defined(HI_TECH_C)
    #include <htc.h>        /* HiTech General Include File */
#endif

#include <stdint.h>        /* For uint8_t definition */
#include <stdbool.h>       /* For true/false definition */

//#fuses NOMCLR,NOPROTECT,NOWDT,INTRC
#pragma config MCLRE=OFF,CP=OFF,WDTE=OFF,FOSC=INTOSCCLK
#define _XTAL_FREQ 4000000
uint8_t sGPIO;

void init()
{
    //Configure GPIO Port
    ANSEL =  0b00000000;  //Configure all GPIO pins as digital
    TRISIO = 0b11111000;  //Set GP0, GP1, GP3 as outputs and the rest as inputs
    WPU = 0x00;           //Disable weak pullups
    //Configuer AD Convertor
    ADCON0 = 0x00;        //AD disabled
    ADRESH = 0x00;        //Init the AD Register
    //Configure Comparator
    CMCON0 = 0xFF;   // Comparator is turned off
    CMCON1 = 0x00;   // Comparator is turned off
    //Interrupt configuration
    INTCON = 0x00;   //Disable all interrupts
}


void main()
{
    uint8_t db_cnt;
    init();
    GPIO = 0b00100000;
    sGPIO= 0b00100000;
    while(1)
    {
     GPIO = 0b00100000;
     for (db_cnt = 0; db_cnt <= 5; db_cnt++)
     {
      GPIO = 0b00100001;
     __delay_ms(40);
     GPIO = 0b00100000;
     GPIO = 0b00100010;
     __delay_ms(40);
     GPIO = 0b00100000;
     GPIO = 0b00100100;
     __delay_ms(40);
     GPIO = 0b00100000;
     }
     __delay_ms(1000);
             for (db_cnt = 0; db_cnt <= 10; db_cnt++)
            {
               __delay_ms(10);
               if (GPIObits.GP5 == 1)
                db_cnt = 0;
            }
     GPIO = 0b00100001;
     __delay_ms(1000);
     GPIO = 0b00100000;
     GPIO = 0b00100010;
     __delay_ms(1000);
     GPIO = 0b00100000;
     GPIO = 0b00100100;
     __delay_ms(1000);
            for (db_cnt = 0; db_cnt <= 10; db_cnt++)
            {
               __delay_ms(1);
               if (GPIObits.GP5 == 1)
                db_cnt = 0;
            }

    }
}

 

No comments:

Post a Comment