miércoles, 7 de noviembre de 2007

Usando Pygame para un Osciloscopio

Pygame no solamente sirve para hacer juegos y demas, lo podemos usar para construir nuestro propio osciloscopio casero de una manera relativamente facil, aqui le paso el codigo .py y asm para que puedan probar modificar a gusto, si bien el programa en pygame le falta es bastante explicativo para que puedan comenzar y mejorar el programa como agregar la base de tiempo por ejemplo, bueno aqui les paso:

* osciloscopio.py


#! /usr/bin/env python
import pygame, serial, sys
from pygame.locals import *
pygame.init()
pygame.font.init()

amarillo = 255, 255, 0
aux = 0
RS232 = serial.Serial('/dev/ttyUSB0', 19200, timeout=1, stopbits=1)
print RS232.portstr
screen = pygame.display.set_mode((512, 256))
pygame.display.set_caption('Osciloscopio')

running = 1
negro = 0, 0, 0

while running:
event = pygame.event.poll()
aux = 0
for x in range(0,512):
if event.type == pygame.QUIT: sys.exit()
RS232.write("AA")
dato = ord(RS232.read())
pygame.draw.line(screen, amarillo, (x, aux), (x+1, dato))
aux = dato
pygame.display.flip()

screen.fill(negro)


* interfas.asm PIC16F873A 20Mhz
;JoseMariaQuiroga
;chispilinux@gmail.com

list p=16f873a
include "p16f873a.inc"
__config _HS_OSC & _WDT_OFF & _LVP_OFF &_DEBUG_OFF & _CPD_OFF &_PWRTE_ON &_LVP_OFF &_BODEN_ON
;-------------------------------------------
CounterA equ 10h
buffer equ 11h
CounterB equ 12h
;-------------------------------------------
org 0h
goto Inicio
org 04h
btfss PIR1,RCIF ;Interrupcion por recepcion?
goto Sale_int_rx
bcf PIR1,RCIF
movf RCREG,w
xorlw 'A' ;Leer el canal AD0 ?
btfss STATUS,Z
goto opcion_a ;jeje si no me fijo
call Receive
movf RCREG,w
xorlw 'A' ;q' canal?
btfss STATUS,Z
goto Sale_int_rx
movlw b'01000001' ;FOSC/8 canal 0 ;Lectura de los AD
movwf ADCON0
call Conversion_ADC
movf buffer,w
call Transmit
goto Sale_int_rx


opcion_a
movf RCREG,w
xorlw 'S'
btfss STATUS,Z
goto opcion_b
call Receive
movf RCREG,w
xorlw 'A' ;q' bit?
btfss STATUS,Z
goto check_sb
bsf PORTB,7
goto Sale_int_rx

check_sb
movf RCREG,w
xorlw 'B' ;q' bit?
btfss STATUS,Z
goto Sale_int_rx
bsf PORTB,6
goto Sale_int_rx

opcion_b
movf RCREG,w
xorlw 'C'
btfss STATUS,Z
goto Sale_int_rx
call Receive
movf RCREG,w
xorlw 'A' ;q' bit?
btfss STATUS,Z
goto check_cb
bcf PORTB,7
goto Sale_int_rx

check_cb
movf RCREG,w
xorlw 'B' ;q' bit?
btfss STATUS,Z
goto Sale_int_rx
bcf PORTB,6
goto Sale_int_rx

Sale_int_rx
retfie
;________________________________________________
; Configurando en PIC
;________________________________________________
Inicio bsf STATUS,RP0 ;Cambiando de banco
bcf OPTION_REG,7
movlw B'00000000' ;Todas las entradas analogicas
movwf ADCON1
movlw B'00111111' ;PORTA input adc
movwf TRISA
movlw B'00000000' ;PORTB output
movwf TRISB
movlw B'10000000' ;PORTC output
movwf TRISC
movlw D'15' ;19200
movwf SPBRG
movlw b'00100000'
movwf TXSTA
movlw b'10010000'
bsf PIE1,RCIE ;Habilitacion interrupcion RX
bcf STATUS,RP0
movwf RCSTA
movlw b'11000000' ;Habilitacion para las
movwf INTCON ;interrupciones generales
;==========================================
mainloop
bsf PORTB,0
goto mainloop
;==========================================
; Subrutinas
;------------------------------------------
Receive btfss PIR1,RCIF
goto Receive
movf RCREG,w
return
;-------------------------------------------
Transmit
movwf TXREG
bsf STATUS,RP0
WtHere btfss TXSTA,TRMT
goto WtHere
bcf STATUS,RP0
return
;-------------------------------------------
; Analogico Digital
;B'01000001' ;FOSC/8 canal 0 RA0
;B'01001001' ;FOSC/8 canal 1 RA1
;B'01010001' ;FOSC/8 canal 2 RA2
;B'01011001' ;FOSC/8 canal 3 RA3
;B'01100001' ;FOSC/8 canal 4 RA4

Conversion_ADC
movlw D'5'
movwf CounterA
delay_0.00002_sec_loop ;20 uS
decfsz CounterA,1
goto delay_0.00002_sec_loop
bsf ADCON0,GO
wait btfsc ADCON0,GO ;Espera q termine la conversion
goto wait
movf ADRESH,w
movwf buffer
return

return
;-------------------------------------------
end
;-------------------------------------------

Este programita en ASM tambien nos sirve para otros ejemplos como el anterior (python-serial).

1 comentario:

Unknown dijo...

¿Como instalo el modulo pygame?, ya lo baje de la pagina de pygame version 32 bits pero aun asi me sale un error al momento de importar: No moduled named pygame tengo usando python 2.7.6

IRC

#freenode->#usljujuy

Seguidores

Eventos n_n

Tira Ecol Nano,Bilo y Luca

Archivo del blog