CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Help me about parity bit sourcecode

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Help me about parity bit sourcecode
PostPosted: Mon Dec 27, 2004 11:34 am     Reply with quote

Please help me "ckielstra" or PCM programmer or anyone can help me.
"ckielstra" !! your source code is not work for me. when i use it by sending data(integer=14) in function it is return 29(integer) to me. Not 0 or 1
why? i'm not well known about asm. (i show my data from lcd)
below is my sourcecode. it's very simple but it's not work.


==============================================
#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=3579545)

#use rs232(STREAM=scard, baud=9600, float_high, xmit=PIN_B0, bits=8, parity=E)
#use rs232(STREAM=scard, baud=9600, float_high, rcv=PIN_B0, bits=9)

#bit ninth_bit = RS232_ERRORS.7

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <input.c>

#define RW PIN_A0
#define RS PIN_A1
#define EN PIN_A2

#define LED0 PIN_A3
#define LED1 PIN_A4

void out_led(int i_led)
{
switch(i_led) // before after
{ // 4 3 4 3
case 0 : { output_high(LED1); output_low(LED0); break; } // 0 0 1 0
case 1 : { output_high(LED1); output_high(LED0); break; } // 0 1 1 1
case 2 : { output_low(LED1); output_low(LED0); break; } // 1 0 0 0
case 3 : { output_low(LED1); output_high(LED0); break; } // 1 1 0 1
default : { break; }
//because PIN_A4 is open Drain output must be toggle from it should be
}
}
//========================================LCD==================================
void busy_strobe(void)
{
OUTPUT_LOW(EN);
delay_us(2);
OUTPUT_HIGH(EN);
delay_us(2);
}

void write_strobe(void)
{
OUTPUT_HIGH(EN);
delay_us(2);
OUTPUT_LOW(EN);
delay_us(2);
}

void Command(void)
{
OUTPUT_LOW(RS);
OUTPUT_LOW(RW);
}

void wait_for_lcd_ready(void)
{
unsigned char busy_flag;

SET_TRIS_C(0XFF); //PORTD7 INPUT;
OUTPUT_LOW(RS); //SET FOR READ BUSY;
OUTPUT_HIGH(RW); //SET FOR READ BUSY;

busy_strobe();
busy_flag=(input_C() & 0x80);

while( busy_flag == 0x80)
{
OUTPUT_LOW(EN);
busy_strobe();
busy_flag=(input_C()&0x80);
}

OUTPUT_LOW(EN);

SET_TRIS_C(0x00); //PORTD7 OUTPUT;
}

void clear_lcd(void)
{
Command();
OUTPUT_C(0x01);
write_strobe();
wait_for_lcd_ready();
}

void cursor_off(void)
{
Command();
OUTPUT_C(0x0c);
write_strobe();
wait_for_lcd_ready();
}

void init_lcd(void)
{
Command(); //add 240847
OUTPUT_C(0x38); //add 240847
write_strobe(); //add 240847
delay_ms(5); //add 240847

Command();
OUTPUT_C(0x38);
write_strobe();
delay_us(120); //add 240847

Command();
OUTPUT_C(0x0f);
write_strobe();
wait_for_lcd_ready();

clear_lcd();

Command();
OUTPUT_C(0x02);
write_strobe();
wait_for_lcd_ready();

cursor_off();
}

void Wchar(unsigned char char_)
{
OUTPUT_HIGH(RS);
OUTPUT_LOW(RW);

OUTPUT_C(char_);
write_strobe();
wait_for_lcd_ready();
}

void gotoxy(unsigned char row,unsigned char col)
{
Command();
if (row==1) OUTPUT_C(0x80+col-1);
else
if (row==2) OUTPUT_C(0xc0+col-1);
write_strobe();
wait_for_lcd_ready();
}

void Wcharxy(unsigned char row,unsigned char col,unsigned char data_)
{
gotoxy(row,col);
Wchar(data_);
}

void string_to_lcd(unsigned char row,unsigned char col,unsigned char *st)
{
unsigned char i,len;

len=strlen(st);
if (len>20) len=20;
gotoxy(row,col);
for(i=0;i<=len-1;i++)
{
Wchar(st[i]);
}
}

void int_to_lcd(unsigned char row,unsigned char col,int data_int)
{
unsigned char str[7];

sprintf(str,"%d",data_int);
string_to_lcd(row,col,str);
}

void long_to_lcd(unsigned char row,unsigned char col,long data_long)
{
unsigned char str[7];

sprintf(str,"%ld",data_long);
string_to_lcd(row,col,str);
}

void hex_to_lcd(unsigned char row,unsigned char col,unsigned char data_hex)
{
unsigned char str[3];

sprintf(str,"%x",data_hex);
string_to_lcd(row,col,str);
}

void float_to_lcd(unsigned char row,unsigned char col,float data_float)
{
unsigned char str[7];

sprintf(str,"%02.1f",data_float);
string_to_lcd(row,col,str);
}
//==========================================================================================

/*
int find_parity1(int data)
{
#asm
swapf data, W
xorwf data, F
rrf data, W
xorwf data, F
btfsc data, 2
incf data, F
movwf _return_
#endasm
}*/

int find_parity(int data)
{
int count;

#asm
movlw 0x8
movwf count
movlw 0
loop:
xorwf data,w
rrf data,f
decfsz count,f
goto loop
movwf _return_
#endasm
}

void main()
{
int8 data,co;
int8 checkp;

port_b_pullups(TRUE);
init_lcd();

checkp=find_parity(0b00001110);
int_to_lcd(1,1,checkp);

while(1)
{
delay_ms(1);
}
}
===============================================

Now i search follow MR. PCM programmer tell me.
and have some courcecode but i''m not sure when i translate in to CCS
Please reveiw and chang in procedure in ccs for me. Please.

below is original from web

===============================================
Parity

From: John Payson
comments from Andrew Warren of Fast Forward Engineering

;8-bit parity
;This routine will leave the parity of X in X.0
;while blenderizing most of the rest of X
swapf X, W ;x = abcdefgh w = efghabcd
xorwf X, F ;x = abcdefgh w = efghabcd
; xor efghabcd
rrf X, W ;x = abcdefgh w = -abcdefg
; xor efghabcd xor -efghabc
xorwf X, F ;x = abcdefgh w = -abcdefg
; xor efghabcd xor -efghabc
; xor -abcdefg
; xor -efghabc

; at this point, the parity for half the bits
; (a, b, e, and f) is in bit 2 of X, and the
; parity for the other half (bits c, d, g, and h)
; is in bit 0 of X.

btfsc X, 2 ; if the parity of (a,b,e,f) is 0,
; then the parity of (a,b,c,d,e,f,g,h)
; is equal to the parity of (c,d,g,h)...
; which is already in bit 0, so skip ahead.


incf X, F ; otherwise, the parity of (a,b,e,f) is 1,
; so the parity of (a,b,c,d,e,f,g,h) is
; NOT equal to the parity of (c,d,g,h).
; invert bit 0.

; at this point, bit 0 contains the parity of
; (a,b,c,d,e,f,g,h).
==============================================
and i change into ccs sourcecode like below
but when i send data is integer=14. it 's return (integer value= -9 for me )
i'm very confuse about asm and register when it use in ccs Crying or Very sad

int find_parity1(int data)
{
#asm
swapf data, W
xorwf data, F
rrf data, W
xorwf data, F
btfsc data, 2
incf data, F
movwf _return_
#endasm
} Crying or Very sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 27, 2004 12:50 pm     Reply with quote

To fix that routine, you need to move "data" into the W register,
and then mask it down so that only bit 0 is valid. See the two
lines that I've added right above the "return" statement at the end.

Code:
int8 find_parity(int8 data)
{
#asm
swapf data, W
xorwf data, F
rrf   data, W
xorwf data, F
btfsc data, 2
incf  data, F
movf  data, W
andlw 1
movwf _return_
#endasm

 
a
Guest







PostPosted: Mon Dec 27, 2004 1:24 pm     Reply with quote

thank you again. MR.PCM programmer and "ckielstra"Very Happy it's work.
i will try your sourcecode and used it for my thesis. i hope u happy in this year and next year. May be. i want your help in the future. thank you very much. you are the light for me. Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group