Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Does PIC support python software

Status
Not open for further replies.

anilvadnala

New Member
Dear Frends,

I am doing a project on accelerometer to find out motion acceleration so that I have to write a C program to PIC18f452.

But acctually I have a programme regarding my project in python language
which I donot understand but I know it is quite related to C.

I am requesting you give any suggesions or help. I am writing the programme in python....


#!/bin/env python
#==========================================================================
#
#
#==========================================================================
# IMPORTS
#==========================================================================
import sys
import time
from aardvark_py import *
#==========================================================================
# CONSTANTS
#==========================================================================
port = 0
bitrate = 100
# Device
DEVICE = 0x18

# Addresses
XOUT_H = 0x00
XOUT_L = 0x01
YOUT_H = 0x02
YOUT_L = 0x03
ZOUT_H = 0x04
ZOUT_L = 0x05
FF_INT = 0x06
FF_DELAY = 0x07
MOT_INT = 0x08
MOT_DELAY = 0x09
CTRL_REGC = 0x0A
CTRL_REGB = 0x0B
CTRL_REGA = 0x0C
# CTRL_REGA
Parity = 0x04
MOTI = 0x02
FFI = 0x01
# CTRL_REGB
CLKhld = 0x80
nEnable = 0x40
ST = 0x20
MOTDen = 0x10
FFDen = 0x08
MOTIen = 0x04
FFIen = 0x02
FFMOTI = 0x01
#==========================================================================
# MAIN PROGRAM
#==========================================================================
# Open the device
handle = aa_open(port)
if (handle <= 0):
print "Unable to open Aardvark device on port %d" % port
print "Error code = %d" % handle
sys.exit()
# User Configuration
Sensitivity = (5.0/(2**12)) # g/count
SampleRate = 250.0 # Hz
sys.stdout.write("Freefall Threshold (g): ")
FF_Threshold = float(sys.stdin.readline())
FF_Threshold = round(FF_Threshold / (16 * Sensitivity))
print "FF_INT byte: 0x%x" % FF_Threshold
print "Freefall Threshold set to %0.3fg" % (16 * FF_Threshold * Sensitivity)
sys.stdout.write("Freefall Interrupt Delay (ms): ")
FF_Delay = float(sys.stdin.readline())/1000
FF_Delay = int(round(FF_Delay * SampleRate))
if FF_Delay < 0x01
FF_Delay = 0x01
if FF_Delay > 0xFF:
FF_Delay = 0xFF
print "FF_DELAY byte: 0x%x" % FF_Delay
print "Freefall Interrupt Delay set to %dms" % ((FF_Delay / SampleRate) * 1000)
sys.stdout.write("Motion Threshold (g): ")
MOT_Threshold = float(sys.stdin.readline())
MOT_Threshold = round(MOT_Threshold / (16 * Sensitivity))
print "MOT_INT byte: 0x%x" % MOT_Threshold
print "Motion Threshold set to %0.3fg" % (16 * MOT_Threshold * Sensitivity)
sys.stdout.write("Motion Interrupt Delay (ms): ")
MOT_Delay = float(sys.stdin.readline())/1000
MOT_Delay = int(round(MOT_Delay * SampleRate))
if MOT_Delay < 0x01:
MOT_Delay = 0x01
if MOT_Delay > 0xFF:
MOT_Delay = 0xFF
print "MOT_DELAY byte: 0x%x" % MOT_Delay
print "Motion Interrupt Delay set to %dms" % ((MOT_Delay / SampleRate) * 1000)
# Ensure that the I2C subsystem is enabled
aa_configure(handle, AA_CONFIG_SPI_I2C)
# Power the EEPROM using the Aardvark adapter's power supply.
# This command is only effective on v2.0 hardware or greater.
# The power pins on the v1.02 hardware are not enabled by default.
aa_target_power(handle, AA_TARGET_POWER_BOTH)
# Activate the pull-up resistor(s)
aa_i2c_pullup(handle,AA_I2C_PULLUP_BOTH);
# Setup the clock phase
#aa_spi_configure(handle, mode >> 1, mode & 1, AA_SPI_BITORDER_MSB)
# Set the bitrate
bitrate = aa_i2c_bitrate(handle, bitrate)
print "Bitrate set to %d kHz" % bitrate
# Configure the device
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGC, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
aa_i2c_write(handle,DEVICE,0,array('B',[FF_DELAY, FF_Delay]))
aa_i2c_write(handle,DEVICE,0,array('B',[MOT_DELAY, MOT_Delay]))
aa_i2c_write(handle,DEVICE,0,array('B',[FF_INT, int(FF_Threshold)]))
aa_i2c_write(handle,DEVICE,0,array('B',[MOT_INT, int(MOT_Threshold)]))
print "Ready..."
while (1):
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGA]))
(ret, data, count) = aa_i2c_read_ext(handle,DEVICE,0,1)
if len(data) < 1:
sys.stdout.write("SENSOR READ ERROR. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
elif data[0] & FFI:
sys.stdout.write("Freefall started. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
elif data[0] & MOTI:
sys.stdout.write("High-g motion detected. Park drive head.\n(Press Enter to resume sampling or Q followed by Enter to quit.) ")
userinput = sys.stdin.readline()
if (userinput == "q\n" or userinput == "Q\n"):
aa_close(handle)
sys.exit()
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, 0]))
aa_i2c_write(handle,DEVICE,0,array('B',[CTRL_REGB, FFIen | MOTIen]))
aa_close(handle)


data sheet of the accelerometer which I am using is kionix company's
KXP84-1050 tri axis digital accelerometer.

Thank you with regards
Anil Vadnala
 

Attachments

  • New Microsoft Word Document.doc
    33.5 KB · Views: 484
Sorry, but you need to write your program in something more mainstream, C18 would be the favourite option as it's free - I doubt Python will ever be used for PIC's.
 
Hi, and welcome to the forum!

First, please place "[ code ][ /code ]" tags (without the spaces) around your code when you post--it will make the posted code retain its formatting so the rest of us can read it.

Second, I agree with Nigel: Python might well be used on a PC or server to interface to the PIC, but the code running on the PIC itself should be written in C or assembler (or BASIC, in some cases). I am not aware of any Python interpreters for the PIC platform.

As Nigel notes, Python, while very mainstream on servers and PCs running *nix or *BSD etc, is not commonly found on microcontrollers. (None, that I know of).


Torben
 
PYthon to ASsembler TRAnslator. At this moment supports Microchip PIC16 instruction set only.

https://sourceforge.net/projects/pyastra/


Even though someone has done the above, I wouldn't seriously consider Python for embedded development, it was never designed for that.

Neat!

That said, I'd say it's mostly "neat" in the sense that it's a cool idea and I'm glad that someone has gone ahead and done it. But like you said, Python wasn't designed for this and it's probably a better idea idea to use C with inline assembly or else pure assembly, especially when timing issues are involved.


Torben
 
@Torben,
You can type
Code:
 and
without it being interpreted. Just place your cursor in the middle of code and hit the bold button. If you hit the quote button all will become clear.

Mike.
 
@Torben,
You can type
Code:
 and
without it being interpreted. Just place your cursor in the middle of code and hit the bold button. If you hit the quote button all will become clear.

Mike.

Hi Mike!

Yeah, I saw that trick in another of your posts last week or so--I just couldn't remember the order off the top of my head and just posted without bothering to check. Lazy, I know. :)


Torben
 
Sorry, but you need to write your program in something more mainstream, C18 would be the favourite option as it's free - I doubt Python will ever be used for PIC's.

Dear Nigel

I just gave the programme which is in python but I have to write C from above programme as PIC18f452 does not support python.

What are you talking about that
Code:
give an example for it plz.
 
@Torben,
You can type
Code:
 and
without it being interpreted. Just place your cursor in the middle of code and hit the bold button. If you hit the quote button all will become clear.

Mike.

Dear Pommie

I am not getting you could you tell me in detail
 
Dear Nigel

I just gave the programme which is in python but I have to write C from above programme as PIC18f452 does not support python.

You have it the wrong way round, it's Python that doesn't support the PIC.

What are you talking about that
Code:
give an example for it plz.

I don't write either Python or C, but you can download the free C18 compiler from MicroChip.
 
Last edited:
but I have to write C from above programme as PIC18f452 does not support python.
That wasn't at all clear from your original posting.

I kind of doubt you will find anyone willing to do the work for you. I'd use the python code as "documentation" to derive your algorithm(s). Then write the C code from there. If you don't know C, there are lots of tutorials out there - google will tell you.

Good luck.
 
But actually I have a program regarding my project in python language which I do not understand but I know it is quite related to C.
Hey Anil. You'll have to repost your code properly. Python has one syntax thing that's different from a lot of other languages - indentation is part of the syntax. It's a bit strange to get used to at first, but for sloppy coders having good coding style forced on you is a good thing.

When you pasted your code here without [code][/code] tags all formatting was lost. I could guess which lines belong in the various code blocks, but I'd probably guess wrong unless I was very familiar with what the original author was doing (I'm not).

EDIT: I did open the .doc file. No formatting there either. Why save it in .doc format if you're going to save it with no formatting anyway? .txt would be just fine, and a lot smaller file.
 
Last edited:
You can type
Code:
 and
without it being interpreted. Just place your cursor in the middle of code and hit the bold button.
You can do it with the font color tool too. Just select half of [code] and make it black instead of red like I did. The [/code] tag doesn't need any treatment. It gets ignored because there's no previous [code] tag.
 
Last edited:
I am doing a project on accelerometer to find out motion acceleration so that I have to write a C program to PIC18f452.

But actually I have a program regarding my project in python language
which I do not understand but I know it is quite related to C.

I am requesting you give any suggesions or help. I am writing the programme in python....

data sheet of the accelerometer which I am using is kionix company's
KXP84-1050 tri axis digital accelerometer.
Your best bet is to dump that Python program and start fresh with your own code. Your accelerometer can communicate with your 18F452 with either I2C or SPI (the Python code is doing I2C). SPI is easier to code for and understand, especially for a newb. Follow the specs in the datasheet and write code to suit.
 
Dear Funz,

I am trying to write a C program for my project of using tri axis accelerometer interfacing to pic18f452 through spi.

While writing I have doubt that I am using digital accelerometer sensor and it has in built adc with in it. So I doubt how to know the analog signal voltage and how to measure acceleration in m/s^2.

Any suggesions please
 
While writing I have doubt that I am using digital accelerometer sensor and it has in built adc within it.
Don't doubt it. The datasheet says it's so, so it must be true. :p Your english is... strange. :D I don't think the word "doubt" means what what you think it means.

So I doubt how to know the analog signal voltage and how to measure acceleration in m/s^2.
You get the analog reading for each axis by sending an "Axis Conversion Command" for the axis you want to read, ignoring the receive byte. Wait 200uS and then send two dummy bytes - the receive bytes will be the 12-bit analog conversion value for that axis, MSB-first, left-justified and zero-padded.

All this is in the datasheet. Start on page 13 for SPI info.
 
Last edited:
Well, I think answer could be "Yes, PIC does support Phyton." !!
And it even supports Java! :)

It's just a matter of getting a python-programmable or java-programmable phone able to communicate with a PIC! :)
There are thousands of cellphones supporting python, and even more supporting java, just look for "pys60" and "j2me".

Connecting them to any PIC should be very easy, and I am currently studying about it: all of these phones have audio headset support, which means they can send and receive audio signals (i.e. voltage signals).

Getting them communicating with a PIC should be just a matter of enabling a PIC to "understand" if the signal is present or not on a pin; a python program on the phone will then "modulate" the sound in a proper way (i.e. you'll have to invent your protocol) to drive the PIC.

I think adding an RC filter to turn sinusoid into constant voltage, and connecting this voltage to a comparator pin in 16f628 should be all we need.

I opened a dedicate thread:
https://www.electro-tech-online.com...ava-on-a-pic-i-just-need-a-little-help.42843/
 
Last edited:
Hmmm, that's a complete stretch. Sure, you can send java or python code to a device that understands it but calling that "support" is bending the meaning of the word past it's breaking point. Heck, you could say a PIC understands nuclear physics because it can spit out "e=mc2".
 
Hmmm, that's a complete stretch. Sure, you can send java or python code to a device that understands it but calling that "support" is bending the meaning of the word past it's breaking point. Heck, you could say a PIC understands nuclear physics because it can spit out "e=mc2".

it was just a method to suggest the power of this possibility....:rolleyes:
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top