The easiest way to communicate from MATLAB to a PIC12C509A

Discussion in 'Embedded Systems and Microcontrollers' started by Dawud Beale, Dec 22, 2014.

  1. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Hi everyone

    I had been intending to send it via RS232 but I have just realised it doesn't have a built in hardware USART.

    Can MATLAB simply output a low and high bit so that I can literally just create a virtual switch in MATLAB? How would I communicate that via a USB?

    I don't need to send or receive a lot of data since I am just flipping a switch so I am not sure if RS232 serial communication is necessary but couldn't think of any easier ways of communicating from MATLAB
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,361
    Continue doing as I have suggested.

    1) Get a CP2102 USB-to-UART bridge.
    2) Plug in the CP2102 and find out which COM port has been assigned to it.
    3) Open the COM port in MATLAB and transmit a single character at whatever baud you choose.
    4) On your MCU, use an interrupt pin to detect the incoming transition.
    5) You can use SW UART techniques to determine the character received.
     
    Dawud Beale likes this.
  3. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Ah so a software UART is the only way to do it?

    You'd think you could set a USB to be permanently high or permanently low and use it as an electronic on off switch
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,361
    No. That is not how USB works.
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,361
    Your SW UART does not need to fully receive the character.
    All you need is a timer-monostable to distinguish between an odd or even character being transmitted.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    17,748
    4,796
    USB is an extremely complex protocol. One way to think of it is that RS-232 is extremely simple and friendly for the developer and often a royal pain for the end user. USB is the other way around in that it is deceptively simple and easy for the end user and a royal pain for the developer.
     
    Dawud Beale likes this.
  7. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    If you have a USB to serial converter and you want to communicate very simple data to a processor but don't want to create a UART, you could just send a 0 or 0xFF, and have the processor detect lowgoing pulses, with the information conveyed by the pulse length.
     
  8. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,694
    904
    This thread sounds like it starts in the middle of another discussion. On the 12F(C)5XX series there are no interrupts.

    I did a project doing about what MrChip recommends, except used polling instead of an interrupt. I then used a counter/timer to pick the bit I needed to use as a switch. Ironically, it really was working as a switch. I can post it here, if that will help, but it is really very simple and crude, as it was my second project post-blinking an LED.

    John

    Edit: The code is in Assembly, so it may have no interest to you.
     
    Last edited: Dec 22, 2014
    Dawud Beale likes this.
  9. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Actually i am coding in Assembley and would definitely benefit from seeing your code if thats ok?
     
  10. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,694
    904
    This code was developed to read the "sign bit" from a DigiMatic dial indicator (Mitutoyo). The protocol is modified SPI (no "ready" is sent). It is not the same protocol you need, but I think the way it was done on the 12F5xx series without interrupts may still be a starting model for you to use. Here is what an oscilloscope shows for the data:

    upload_2014-12-23_6-51-59.png

    Puling the Request (REQ) low starts the transmission. I monitor the clock and count 20 pulses to get the SIGN BIT. That told me whether the indicator had passed zero. In use, I set the indicator at zero where I wanted the machine (lathe) to stop. Then backed off to start threading. It was built for right-hand threading toward the headstock on a lathe without half-nuts. When zero was reached the device shut down the lathe. That allowed me to concentrate on tapering the final thread.

    I hope the code is readable. The delay routines are straight from PicList. I am now using a macro from Mike McLaren (K8LH). Also, I use his software USART at 9600 baud for another device. I will try to find those links and post them as edits here.

    The code may look terrible. Download it to MPLAB and set gutter=4, tab=5, line =80 Pommie has provided a nice little program to make displayed code look better (CodeTidy), but I have not installed it on my current PC yet.

    John

    Edit: Mike McLaren (Mike-K8LH at ETO) put both his delay routine for 12-bit core devices like the 12F5xx and the serial communication code in post #14 here: http://www.electro-tech-online.com/threads/indirect-data-addressing-when-done.124301/#post1028425
     
    • AAC.asm
      File size:
      3.5 KB
      Views:
      17
    Last edited: Dec 23, 2014
  11. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Hi everyone, just a quick question following on from this. I have acquired a USB extension lead as suggested, and a USB-to-RS232 communication device, and have found some MATLAB code that should potentially be transmitting some RS232 data. So I want to check what I have so far actually works. So from MATLAB, to the USB port, to the USB extension cable, to the USB to RS232 device, and out of the 5 pins from that device, I should potentially seeing the data I initially transmitted from MATLAB. So I would like to hook the RS232 device to an oscilloscope to view the signal. As there are 5 pins, +5V, +3.3V, GND, Tx and Rx, am I right in thinking that I can simply hook the oscilloscope up to the Rx and the oscilloscope GND to RS232 GND to view the transmitted data?
     
  12. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    it is very important to know whether you have an RS232 port or a logic-level serial port! Your processor would not enjoy being connected to +/-12V levels. But if you're going to check it on a scope, presumably you'll find out which you have.
     
  13. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
  14. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,361
    What you have will interface to 0-5V TTL levels, not RS-232 voltage levels.

    I wouldn't know what they labeled as RXD and TXD. You could find out by tracing the PCB or using the oscilloscope.

    There are simple tests you can perform without getting into MATLAB.

    Open the Device Manager on your PC and expand the Ports and USB tree. Then plug in the USB CP2102 adapter.
    Observe that the device is recognized and note which COM port has been assigned.

    Then open Hyperterminal, set up for that COM port and try to send a character by typing any key (I like to use uppercase U. Observe the RXD and TXD lines on the scope.
     
    Last edited: Jan 6, 2015
  15. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    OK, there's no need to look at the entire page: the title comes up saying "USB to TTL Serial Adapter". It's common for people to to call it RS232, but it's not correct and could lead to electrical problems.
     
  16. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
  17. jpanhalt

    AAC Fanatic!

    Jan 18, 2008
    5,694
    904
    These?

    upload_2015-1-15_15-39-35.png

    Those 6 pins are just an alternative way to attach the ICSP cable. Pin 1 is nearest you (MCLR). Pin 6 is not used.

    The datasheet for the ZIF adapter will tell you how to set up the jumpers to get signal and power to the correct pins for whatever chip you are using.

    John
     
Loading...