Wednesday, 9 July 2014

PIC to PIC Communications

Introduction

Microchip have released a new family of 18F microcontroller devices that support a new feature called PPS-Lite. Peripheral Pin Select (PPS) allows the various digital IO functions such as UARTs onto pins of your choice.

In this post I will show you how to communicate between two PICs using SPI and how to set up the  PPS registers.


Schematic


Both PICs are the same type 18F65J94, one is designated the Master the other the SLAVE. The master and slave SPI pins are connected as shown in the image below. Note that here we do not use the slave select line.



Source Code

I wanted to be able to use the same binary for both PICs to help make programming of both chips easier as there is only one choice of hex file. The PICs will check pin E1 to see if they are a master or slave and behave accordingly.

One difficulty I faced is that the Master SCLK is an output and the Slave SCLK is an input. So SCLK is actually two different hardware functions SCLKOUT and SCLKIN. PPS uses two types of registers, output and input, so I needed to set the master clock pin to output register RPOR23 and the slave to input register RPINR8. Luckily the CCS compiler makes setting up these registers easy and I was able to use the same code/binary for both pics:

#pin_select SDO1=PIN_D1
#pin_select SDI1=PIN_D4
#pin_select SCK1OUT=PIN_D3
#pin_select SCK1IN=PIN_D3


The code below is written for the CCS Compiler v5.025, MPLAB X v2.10. The master will send 0,1,2 and 3 every second via SPI. Both pics will illuminate the red/green leds as they send/receive the data.


 #include "18F65J94.h"  
 #include "Device.h"  
 #inline  
 void slave(void);  
 #inline  
 void master(void);  
 #inline  
 void slave_spi_init(void);  
 #inline  
 void master_spi_init(void);  
 void main(void) {  
     set_tris_e(TRISTATE_E);  
     if (input(MCU_ID)==ID_MASTER)  
     {  
         output_bit(LED_RED,LED_OFF);  
         output_bit(LED_GREEN,LED_ON);  
         delay_ms(1000);  
         master();  
     }  
     else  
     {  
         output_bit(LED_RED,LED_ON);  
         output_bit(LED_GREEN,LED_OFF);  
         delay_ms(500);  
         slave();  
     }  
 }  
 #inline  
 void slave_spi_init(void)  
 {  
     //Set SDI and SCK as inputs, SDO as output  
     set_tris_d(TRISTATE_D_SLAVE);  
     //SPI Setup  
     //Slave, slave select disabled  
     //Clear errors  
     SSP1CON1 = 0b00000101;  
     //Slave must clear Sample bit  
     SSP1STAT_SMP=0;  
     //Mode 0 CKP = 0, CKE = 1  
     //Mode 1 CKP = 0, CKE = 0  
     //Mode 2 CKP = 1, CKE = 1  
     //Mode 3 CKP = 1, CKE = 0  
     SSP1CON1_CKP = 0;  
     SSP1STAT_CKE=0;  
     //Enable SPI  
     SSP1CON1_SSPEN=1;  
 }  
 void master_spi_init(void)  
 {  
     set_tris_d(TRISTATE_D_MASTER);  
     //SPI Setup  
     //Master, clock = FOSC/64  
     //Clear errors  
     SSP1CON1 = 0b00000010;  
     //Sample in middle  
     SSP1STAT_SMP=0;  
     //Mode 0 CKP = 0, CKE = 1  
     //Mode 1 CKP = 0, CKE = 0  
     //Mode 2 CKP = 1, CKE = 1  
     //Mode 3 CKP = 1, CKE = 0  
     SSP1CON1_CKP = 0;  
     SSP1STAT_CKE=0;  
     //Enable SPI  
     SSP1CON1_SSPEN=1;  
 }  
 void set_led(int value)  
 {  
     switch(value){  
         case 1:  
             output_bit(LED_RED,LED_ON);  
             output_bit(LED_GREEN,LED_OFF);  
             break;  
         case 2:  
             output_bit(LED_RED,LED_OFF);  
             output_bit(LED_GREEN,LED_ON);  
             break;  
         case 3:  
             output_bit(LED_RED,LED_ON);  
             output_bit(LED_GREEN,LED_ON);  
             break;  
         default:  
             output_bit(LED_RED,LED_OFF);  
             output_bit(LED_GREEN,LED_OFF);  
             break;  
     }  
 }  
 void slave(void)  
 {  
     slave_spi_init();  
     while(true)  
     {  
         SSP1BUF='X';  
         while(!SSP1STAT_BF){}  
         set_led(SSP1BUF);  
     }  
 }  
 void master(void)  
 {  
     master_spi_init();  
     delay_ms(500);  
     int buf,i;  
     while(true)  
     {  
         for (i=0;i<4;i++)  
         {  
             SSP1BUF=i;  
             set_led(i);  
             while(!SSP1STAT_BF){}  
             buf = SSP1BUF;  
             delay_ms(1000);  
         }  
     }  
 }  




 #ifndef DEVICE_H  
 #define    DEVICE_H  
 //HS External Osc, FRC internal (Fast RC Oscillator)  
 #fuses HS  
 //T1OSC/SOSC Secondary Oscillator, low power circuit selected  
 #fuses SOSC_LOW  
 //Extended Instruction Set  
 #fuses XINST  
 //Stack Overflow / Underflow Reset Enable  
 #fuses STVREN  
 //Brown out detect: Resets the device if the voltage chipped  
 // Useful for when the user switches on/off quickly and the PIC needs resetting  
 #fuses BORV20  
 //Internal External Switch over: IESO,NOIESO  
 // IESO: Two speed start up mode is enabled. Used for power saving  
 #fuses NOIESO  
 //Code Protection: PROTECT, NOPROTECT  
 //    Use NOPROTECT otherwise chipped will be permantly programmed  
 #fuses NOPROTECT  
 //CLK0 Output enabled on RA6  
 #fuses CLOCKOUT  
 //PLL Frequency Multiplier  
 #fuses NOPLL  
 //Primary Oscillator - none  
 #fuses PR  
 //No clock switching, no fail safe clock monitor  
 #fuses NOCKSNOFSM  
 //Write Protect Flash Page  
 #fuses WPFP  
 //Segment Write Diabled  
 #fuses WPDIS  
 //TMR5 Gate is driven by the T5G input  
 #fuses T5G_IS_T5G  
 //C1INA and C3INA are on their default pin locations  
 #fuses CINA_DEFAULT  
 //IOLOCK One-way set enable  
 #fuses IOL1WAY  
 //USB Low speed clock select  
 #fuses LS48MHZ  
 //MSSP2 7-bit address masking mode enable  
 #fuses MSSP2MSK7  
 //MSSP1 7-bit address masking mode enable  
 #fuses MSSPMSK7  
 //Watch dog timer window  
 #fuses WDTWIN_25%  
 //Watch dog timer clock source  
 #fuses WDTCLK_FRC  
 //Watch dog timer postscaler  
 #fuses WDT32768  
 //Watchdog Timer:  
 #fuses NOWDT  
 //Windowed watch dog timer disable  
 #fuses NOWINDIS  
 //Watch dog prescaler  
 #fuses WPRES128  
 //Voltage Regulator Control  
 #fuses NOVREGSLEEP  
 //VBAT BOR Enable  
 #fuses VBATBOR  
 //Deep sleep BOR Enable  
 #fuses NODSBOR  
 //Deep sleep  
 #fuses NODS  
 //In-Circuit Debugger Mode: DEBUG, NODEBUG  
 // DEBUG - In circuit debugging enabled, RB6 ICSPCLK & RB7 ICSPDAT are dedicated to the debugger  
 #fuses NODEBUG  
 #device PASS_STRINGS=IN_RAM  
 #use delay(clock=20MHz)  
 //Write/read to a port directly, the compiler will not set the tristate  
 #use FAST_IO(ALL)  
 #define TRISTATE_E                    0b00000010  
 #define TRISTATE_D_MASTER            0b00010000  
 #define TRISTATE_D_SLAVE            0b01011000  
 #define LED_RED                        PIN_E4  
 #define LED_GREEN                    PIN_E5  
 #define MCU_ID                        PIN_E1  
 #define SPI_SDO1                    PIN_D1  
 #define SPI_SDI1                    PIN_D4  
 #define SPI_SCK1                    PIN_D3  
 #define ID_MASTER                    1  
 #define ID_SLAVE                    0  
 #define LED_ON                        1  
 #define LED_OFF                        0  
 /*  
  *            SPI Configuration  
  * MASTER  
  *        SDO1    Output        PIN_D1        RP21        RPOR20_21=0x40  
  *        SCK1    Output        PIN_D3        RP23        RPOR22_23=0x30  
  *        SDI1    Input        PIN_D4        RP24        RPINR8_9=0x6F  
  *  
  * SLAVE  
  *        SDO1    Output        PIN_D1        RP21        RPOR20_21=0x40  
  *        SCK1    Input        PIN_D3        RP23        RPINR8_9=0x05 !  
  *        SDI1    Input        PIN_D4        RP24        RPINR8_9=0x60 !  
  *  ! write 0x65 to RPINR8_9  
  *  
  */  
 #pin_select SDO1=PIN_D1  
 #pin_select SDI1=PIN_D4  
 #pin_select SCK1OUT=PIN_D3  
 #pin_select SCK1IN=PIN_D3  
 #byte SSP1CON1 = getenv("sfr:SSP1CON1")  
 #byte SSP1STAT = getenv("sfr:SSP1STAT")  
 #byte SSP1BUF = getenv("sfr:SSP1BUF")  
 #bit SSP1STAT_SMP = SSP1STAT.7  
 #bit SSP1STAT_CKE = SSP1STAT.6  
 #bit SSP1STAT_BF = SSP1STAT.0  
 #bit SSP1CON1_WCOL = SSP1CON1.7  
 #bit SSP1CON1_SSPOV = SSP1CON1.6  
 #bit SSP1CON1_SSPEN = SSP1CON1.5  
 #bit SSP1CON1_CKP = SSP1CON1.4  
 #endif    /* DEVICE_H */  

Friday, 6 June 2014

Privacy Policy

This post is about the Privacy Policy for Pig Dog Bay apps, including CleverDic Crossword Solver, Anagram Solver, Weight Tracker, Mortgage Calculator and RoboQuip.

I am a sole developer based in the UK, I produce Android apps which I distribute through Google Play under the developer name Pig Dog Bay. My apps may contain advertising delivered via Google Admob, I trust that Admob will not harm you or you're device but please let me know if you have concerns about any ads that are shown.

Your privacy is important to me and I do not collect or store any of your personal information. My apps will however gather anonymous usage statistics viaGoogle Play Services, Google AdMob and Google Analytics. These statistics are completely anonymous to me and allow me to monitor such information as the daily Google Play downloads, user interface events (like pressing buttons), the number of clicks on ads and show in real time the number of users and their location.

Note that comments posted to the Pig Dog Bay Facebook, Google Play, Twitter and Blogger pages are public and may show your name and photo.


Disclaimers

My software is provided 'as is' without warranty of any kind, either express or implied, including, but not limited to, the implied warranties of fitness for a purpose, or the warranty of non-infringement. Without limiting the foregoing, I make no warranty that:


  1. the software will meet your requirements
  2. the software will be uninterrupted, timely, secure or error-free
  3. the results that may be obtained from the use of the software will be effective, accurate or reliable
  4. the quality of the software will meet your expectations
  5. any errors in the software will be corrected.

The Weight Tracker App is for information purposes only and is not intended to be used for medical purposes. I am not a doctor or have any medical training.
Warning, CleverDic Crossword Solver and Anagram Solver apps use large dictionaries and so may contain words that may be offensive to you. Any offence caused is completely unintended.

Finally
I am a software engineer, not a lawyer! I write apps for fun, learning, a little money and the hope that many others will enjoy using my apps. 

Pig Dog Bay

Monday, 26 May 2014

Anagram Solver v1.03.04

A new version of the Anagram Solver App is now available from Google Play.


The app has a new faster anagram search algorithm which means the sub-anagram search is now 3 letters deep.


The deeper word search is a great new feature for word wheel games and TV shows like Countdown. For fun, enter you name and see what sub-anagrams can be made!

Under the bonnet the app now uses the Google Play SDK which gives me access to real time usage statistics via Google Analytics APIs.

In the pipeline is an exciting new feature, Supergrams. Enter abcdef+++ and the app will search for all the nine letter words that contain abcdef. Very useful for cryptic crosswords where you know some of the letters.





Never be puzzled again!
Pig Dog Bay
 


Wednesday, 19 February 2014

RoboCop Film Review



As science fiction the film was excellent exploring themes such as machine versus human intelligence, machine control over life or death decisions and the moral ethics of cyborg technology. As a RoboCop remake, it just paid lip service to the original and made the overall plot very clunky. The characters were flaccid irrelevant versions of their earlier counter parts. The satire of the original replaced with a biased mainstream media mouthpiece, played by Samuel Jackson, who frankly was just annoying.



The film starts showing ED-209's and other robotic soldiers patrolling around Tehran, the overtone was clearly American Imperialism. During a live television interview a band of locals launch suicide attacks at the ED-209s, an irate Iranian kid with a bread knife then confronts a massive ED-209 which quickly decides he is a threat and the kid is promptly blown away. I don't know why but I chuckled!

Apparently these cold killer droids operate all over the world, but not in the USA because America is special (puke!) and a certain senator says he doesn't want machines making life or death decisions. This simply serves as a cringing plot point so that OCP, the big bad ass corporation, have to come up with a cute lovable cyborg version of the killer robots.



In the earlier film it was fascinating seeing through RoboCop's point of view as he was being made, the booting up, the scientists fixing him and Bob Mortimer getting his hand crushed - "You're gonna be one bad motherf*****". In this version Murphy wakes up confused from a sweet dream but then panics when he realise he's a machine. Gary Oldman who plays the Frankenstein chief Scientist tries to calm his creation down but lets him roam out of the lab. This is clearly not Detroit as he wanders through a huge Foxconn like manufacturing plant in China. A prescient politic touch showing how economic power and technological prowess has moved out of places like Detroit to the Far East.




The film delves in to the robotics and science of the cyborg tech which is great. You are shown how RoboCop is put together and that the only human part is the brain, face and lungs. Unfortunately the human part makes RoboCop slower than the droid soldiers and so the chief scientist comes up with a clever trick. During combat computer control will take over but the human subconcious will think it is making the decisions. Considering that the movie started out by making the case that a human needs to be pulling the trigger, the legal and moral implication that a machine is still in control is skipped over. Hmmm! The Seargeant at Arms in this segment is the only character I liked, with his gung-ho attitude and jokes about RoboCop being a Wizard of Oz Tinman.



Over at OCP headquarters the marketing guy is wetting his pants over the feedback from the Robocop focus groups. Michael Keaton, the CEO, puts his infinite wisdom into action and demands to see a more tactical makeover, make him black. These pair ain't no Dick Jones or Bob Mortimer, think more Laurel and Hardy, here the film begins to disappoint.




There's no Clarence "Can you fly Bobby?" Bodicker, a very nasty vile bad guy, up there with Christopher Walken in a View to A Kill. In this remake all I can tell you about the villain is that he had weird gingery sideburns. The criminal element is just a sub-plot to explain how Murphy was killed and they are quickly dispensed with when RoboCop gets going. This means ultimately that Michael Keaton's (can't remember his character's name!!) characters only crime is that he ordered RoboCop to be switched off whilst he told Murphy's wife some porkies that he had died due to a malfunction.

Needless to say, RoboCop isn't going to die. Cue 15 minutes of cartoonish, low violence CGI as RoboCop hops around ED-209s. Yawn. No bad guy falling into toxic waste here and getting splattered. Just Keaton panicking and then getting shot. In the finale, a battered RoboCop is rebuilt ready for a bunch of sequels.

Considering we now have drones that kill people and companies like Google (Cyberdyne Systems?) moving headstrong into AI and robotics, we are quickly approaching the AI singularity. This film asked important questions about superior machine intelligence, its application and the moral implications it will have. One slogan in the film said "People Need Jobs Robots don't". Here we have a great sci-fi film clumsily shoe-horned into a RoboCop retelling.





I'd wouldn't buy that for a dollar!
Pig Dog Bay

Thursday, 13 February 2014

Mortgage Calculator

My first ever app has been re-released, Mortgage Calculator. Faster-Easier-Better Looking!


  • New exciting background colours.
  • Fast data entry - no typing
  • Share details with other apps such as text messaging
As you can see the App now uses number pickers. These are the same intuitive custom controls as used on the successful Weight Tracker app.  This makes the data entry fast and free from typing. Notice too that the monthly repayment is instantaneously calculated.

A slightly embarrassing bug fix to mention tho, the mortgage formula I used previously was incorrect.  Amazingly the BBC Mortgage Calculator  I checked it against, gave the same answer as the faulty formula. The BBC mortgage calculator still hasn't been fixed yet!!!

One feature which I think will be very popular is the Share option. You can send the mortgage details as an SMS text message, an e-mail or to a social media app if you have any of these apps installed.


Download Mortgage Calculator from Google Play


Pig Dog Bay

Tuesday, 11 February 2014

Flappy Bird


I recently posted about a national scheme, Year of Code that encourages people to get into coding. I bet Flappy Bird will have a bigger impact than Year of the Code, because the game earned $50,000 each day and it took only 3 days to code (as claimed by the developer) . There must be an whole bunch of people out there thinking $50,000 a day for such a simple game!

If you are a novice programmer, welcome! Don't get disheartened when you realise it's going take you longer than 3 days! Heck you'll spend a week getting Eclipse and Android SDK up and running. Most disheartening of all is that apps better than Flappy Bird will get hardly any downloads and earn a pittance, in fact this is the case for most apps on the market. Why some apps go viral is a mystery and seems more like a lottery.

To see how long it takes to create an Android game, I considering creating an App based on the Atari Classic Dodge 'Em.




Before I begin, I need to consider the following:


  1. How will the user control the car, on screen buttons, swipe or tilt? I will have to spend a lot time play testing to see what works best.
  2. Clearly the graphics need to be updated, I'm pretty poor at graphic design so I still need to keep it simple, I think I can manage with Dodge 'Em tho!
  3. Shall I use a game engine or roll my own. Either way I've got some learning to do.


And lastly I need some spare time!!
Pig Dog Bay




Thursday, 6 February 2014

Robocop App


In commiseration of the unnecessary remake of Robocop, RoboQuip is back!



Its big! Its back! RoboQuip! A Pig Dog Bay Tradition

Featuring a whole new user interface - see the quotes appear as Murphy would see them! 

See the quotes scroll up the console!



Not since the days of the ZX Spectrum has a RoboCop app been this good!

Serve the public trust


Now take laughing boy here and download RoboQuip. If Bobby had this App he could have flown!


Stay out of trouble!


Good Apps are where you find em. Give the man an App!
Pig Dog Bay Cop!